从服务中调用正在运行的活动中的方法

11

我目前正在开发一个Android项目。

到目前为止,我已经实现了Firebase,特别是FirebaseInstanceIdService和FirebaseMessagingService:

public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";

private Context context;

@Override
public void onTokenRefresh() {
    context = getApplicationContext();
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.e(TAG, "Refreshed token: " + refreshedToken);

    SharedPreferences sharedPreferences = context.getSharedPreferences("token", context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("token", refreshedToken);
    editor.commit();

    sendRegistrationToServer(refreshedToken);
}

/**
 * Persist token to backend.
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    SendRegistrationKey task = new SendRegistrationKey(context);
    task.execute(token);
}

}

public class MessagingService extends FirebaseMessagingService {

private static final String TAG = "MsgService";

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

        sendNotification(remoteMessage);
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]

private void sendNotification(RemoteMessage remoteMessage) {
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    PushNotificationManager PNManager = PushNotificationManager.getInstance(getApplicationContext());
    PNManager.buildNotification(notification.getTitle(), notification.getBody());
}

我想要实现的是:

当应用程序在后台运行时,我只想在通知中心中收到简单的通知。(这已经可以工作了)

但是,当应用程序在前台并且正在运行时,我希望有不同的行为: 我想要消耗推送通知并显示一个警报。

但我的问题是:如何从服务与正在运行的活动交互,或者实现所需的行为的正确方法是什么? 一定有一个简单的解决方案,对吧?

谢谢!


从Service发送广播,然后在Activity中监听广播。 - X3Btel
4个回答

5

在应用程序类中编写以下代码:

 public Context currentactvity = null;
public Context getCurrentactvity() {
    return currentactvity;
}

public void setCurrentactvity(Context currentactvity) {
    this.currentactvity = currentactvity;
}

在每个活动的onResume方法中编写此代码,在onPause方法中将其设置为null。

 // in onresume
MyApplication.getInstance().setCurrentactvity(this);

// in onpause
MyApplication.getInstance().setCurrentactvity(null);

现在你可以从服务类中调用活动方法。
  if (MyApplication.getInstance().getCurrentactvity() != null && MyApplication.getInstance().getCurrentactvity() instanceof youractivityname) {
            ((youractivityname) MyApplication.getInstance().getCurrentactvity()).youmethodname(parameter);

        }

太好了,这正是我在寻找的 (y) - kauppfbi

2
如何从服务与正在运行的活动进行交互,或者实现预期行为的正确方法是什么?
最好的方法是使用事件总线(例如GreenRobot's or local broadcast)。您的活动在onResume()中注册侦听器(并在onPause()中取消注册),您的服务只需在时机成熟时广播消息即可。
主要优点是保持两个元素(服务和活动)完全分离。您还可以避免最糟糕的解决方案-直接调用活动的方法。
要了解您是否处于后台状态,最好利用应用程序的GreenRobot'sActivityLifecycleCallbacks - 强制活动报告这一点没有任何好处。

1

尝试使用以下代码:

该方法会检查所有正在运行的应用程序,并返回当前应用程序是否处于后台状态或前台状态的 true 或 false。

    public static boolean isAppIsInBackground(Context context) {
        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())) {
                            isInBackground = false;
                        }
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equals(context.getPackageName())) {
                isInBackground = false;
            }
        }

        return isInBackground;
    }

上述方法检查正在运行的应用程序,并返回当前应用程序是否处于后台状态或前台状态的 true 或 false。 - Divyesh Patel
哪些信息以及在哪里? - Divyesh Patel

0

尝试一下

1. 在您的活动中注册自定义广播接收器

context.registerReceiver (myBroadcastReceiver, new IntentFilter ("CUSTOM_ACTION"));
private final BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver () {

    @Override
    public void onReceive (Context context, Intent intent) {

        //Do something
    }
};

2. 从你的服务中发送广播

Intent intent = new Intent ("CUSTOM_ACTION");
context.sendBroadcast (intent)

+1。感谢更新。已更新答案。 在运行时通过调用Context.registerReceiver()创建接收器,而不是在清单中声明接收器。 https://developer.android.com/preview/features/background.html#broadcasts - PEHLAJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接