[英]How can i get a value to class from another class
我想從activeroom.java獲取一個roomid方法的值。 我想我可以使用getClass()。getMethod()。 ?
因此,此代碼在NotficationUtils.java中,並嘗試從ActivityRooms.java獲取“ isRoomId”方法的字符串值
public static boolean isAppIsInBackground(Context context,String chatRoomId) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
在if()旁邊,我正在定義m方法,我想從“活動類”中獲取值“ isRoomId”方法。
try {
Method m = activeProcess.getClass().getMethod("isRoomId",null);
if (chatRoomId.equals(m.toString()))
{
isInBackground = false;
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
ActivityRoom.java具有isRoomId方法。
public static String chatRoomId;
chatRoomId = intent.getStringExtra("chat_room_id");
public static String isRoomId()
{
return chatRoomId;
}
ActiveRoom
類的isRoomId()
方法不是靜態方法。 因此,您需要實例化該類,然后調用該對象的方法。
如果isRoomId
返回的值與ActiveRoom
類的對象狀態無關,則可以將其設置為如下所示的靜態方法。 然后,您可以從NotficationUtils.java
調用此方法
方法
public static String isRoomId()
{
String chatRoomId="6";
return chatRoomId;
}
開票
ActiveRoom.isRoomId()
如果您計划在isRoomId()中編寫的邏輯取決於對象狀態,那么您需要在NotificationUtil類中獲取相關的對象引用並在該對象上調用方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.