I have a service gcm in the background, how do I know if the app is open or not?

微笑、不失礼 提交于 2019-12-12 04:50:16

问题


I have a class that extends GCMBaseIntentService, when I get a message from gcm function:

@Override

 protected void onMessage (Context context, Intent intent)
 {
     String message = intent.getExtras().GetString("alien");
     generateNotification (context, message);
 }

I wish that when a new message arrives the app recognize if the application is open and the user is using, or is a simple background service, and therefore I see a notification. I need to know if it is in the background or foreground, any suggestions ??


回答1:


when a new message arrives the app recognize if the application is open and the user is using, or is a simple background service, and therefore I see a notification

To do that, you can use an in-process event bus. Have the service post an event to the bus. Have the UI subscribe to the bus for those events when the UI is in the foreground (e.g., register in onResume(), unregister in onPause()). Have the UI process the events when the UI gets them. If the UI does not respond to the event, the service can then raise a Notification.

I have sample apps that demonstrate this for three popular event bus implementations for Android:

  • LocalBroadcastManager: https://github.com/commonsguy/cw-omnibus/tree/master/EventBus/LocalBroadcastManager

  • greenrobot's EventBus: https://github.com/commonsguy/cw-omnibus/tree/master/EventBus/GreenRobot

  • Square's Otto: https://github.com/commonsguy/cw-omnibus/tree/master/EventBus/Otto




回答2:


A pretty common approach to solving this problem is using an event bus to publish the message to the rest of your app to see if anyone is registered to handle it.

A good event bus for Android is the greenrobot EventBus https://github.com/greenrobot/EventBus

A code example of how to do it:

Create a class for your message

public class MessageEvent { 
    public message;

    public MessageEvent(String message) {
        this.message = message;
    }
}

Add the EventBus to your BroadcastReceiver

protected void onMessage (Context context, Intent intent) {
    String message = intent.getExtras().getString("alien");

    MessageEvent event = new MessageEvent(message);

    EventBus.register(this);
    EventBus.getDefault().post(event)
}

public void onEvent(NoSubscriberEvent event) {
    if (event.originalEvent instanceOf MessageEvent) {
        generateNotification(((MessageEvent) event.originalEvent).message));
    }
}

Then, in your Activity:

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

// This method will be called on the main thread when a MessageEvent is posted
public void onEventMainThread(MessageEvent event){
    Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}

This way, if your activity is active, it can process the MessageEvent, if it is not active, you can display the notification as usual.




回答3:


GCM runs completely at background and not in foreground.

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

You will use these two permission's which is to receive the message at background.

Please read this.

https://developer.android.com/google/gcm/client.html



来源:https://stackoverflow.com/questions/27582335/i-have-a-service-gcm-in-the-background-how-do-i-know-if-the-app-is-open-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!