Check if app is open during a GCM onMessage event?

允我心安 提交于 2020-01-20 03:25:10

问题


I am wondering how to check if my application is open and currently visible to the user when receiving an onMessage() from GCM. At first, I was just using my own boolean isVisible, but then I realized this isn't reliable, because if the app isn't open, the object I use to access that flag is null. While this in itself could be used to see if the app is open, it seems a little bit messy. Is there a way in Android from a system level to somehow check if the application is currently open, and if the user is viewing the app? Keep in mind an app could technically be running, but not be visible, because a user has recently pressed the "home" button sending it to the background.

@Override
protected void onMessage(Context arg0, Intent arg1) {
    String turn = intent.getExtras().getString("turn");
    if (turn.equals("yours"){
         if (/*app is open*/){ <------------------ what can go here?
             // dont generate a notification
             // display something in the game instead
         }
         else{
             // generate notification telling player its their turn
         }
    }
}

回答1:


I would use order broadcasts to do that.

In your onMessage method:

Intent responseIntent = new Intent("com.yourpackage.GOT_PUSH");
sendOrderedBroadcast(responseIntent, null);

In your Activity:

public class YourActivity extends Activity {

    final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //Right here do what you want in your activity
            abortBroadcast();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //.....
    }

    @Override
    protected void onPause() {
        unregisterReceiver(mBroadcastReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH");
        filter.setPriority(2);
        registerReceiver(mBroadcastReceiver, filter);
        super.onResume();
    }
}

The other BroadcastReceiver

public class SecondReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //In this receiver just send your notification
    }

}

Manifest:

<activity
    android:name=".YourActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action
            android:name="android.intent.action.MAIN" />
        <category
            android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<receiver
    android:name=".SecondReceiver">
    <intent-filter
        android:priority="1">
        <action
            android:name="com.yourpackage.GOT_PUSH" />
    </intent-filter>
</receiver>

Basically in the onMessage method you send an Intent which is first received by the BroadcastReceiver registered inside YourActivity if it is running and in foreground, otherwise it is received by the SecondReceiver.




回答2:


Use SharedPreferences saving the boolean isVisible, and when you get the value from the preference you can add a default value.

SharedPreferences settings = context.getSharedPreferences("NAME_XXX", Activity.MODE_PRIVATE);
settings.getBoolean("visible", false);



回答3:


What I always do is have a reference to the current Activity.

I set the current Activity in every onResume to this and set it to null in every onPause.

If the current Activity is null then the app is not open. If it's not null you can see if the correct Activity is open and deliver it to that Activity.

GCMIntentService:

public static Activity currentActivity;
public static final Object CURRENTACTIVIYLOCK = new Object();

    @Override
protected void onHandleIntent(Intent intent) {
    synchronized(CURRENTACTIVIYLOCK) {
        if (currentActivity != null) {
            if (currentActivity.getClass() == CorrectActivity.class) {
                CorrectActivity act = (CorrectActivity)currentActivity;
                act.runOnUiThread(new Runnable() {
                    public void run() {
                        // Notifiy activity
                    }
                });
            } else {
               // show notification ?
            }
        } else {
            // show notification
        }
    }
}

CorrectActivity:

@Override
    protected void onResume() {
        synchronized (GCMIntentService.CURRENTACTIVITYLOCK) {
                GCMIntentService.currentActivity = this;
            }
        }
        super.onResume();
    }

@Override
    protected void onPause() {
        synchronized (GCMIntentService.CURRENTACTIVITYLOCK) {
            GCMIntentService.currentActivity = null;
        }
        super.onPause();
    }



回答4:


The thing that worked for me:

Create a final Class Constants, inside it, create static varaiable:

public final class Constants{
public static AppCompatActivity mCurrentActivity;
}

Now, on each on resume of your activties say:

    @Override
    protected void onResume() {
        super.onResume();
        Constants.mCurrentActivity = this;
    }

When receieving notification, check if current activity is null, if its null, application is not opened, if activity isn't null, you can check things like:

if(Constants.mCurrentActivity instanceof MainActivity){
   ((MainActivity) Constants.mCurrentActivity).yourPublicMethodOrStaticObject;
}


来源:https://stackoverflow.com/questions/15819663/check-if-app-is-open-during-a-gcm-onmessage-event

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