在GCM onMessage事件期间检查应用是否已打开?

时间:2013-04-04 19:06:59

标签: java android google-cloud-messaging

我想知道在从GCM收到onMessage()时,如何检查我的应用程序是否已打开并且当前对用户可见。起初,我只是使用自己的boolean isVisible,但后来我意识到这不可靠,因为如果应用程序没有打开,我用来访问该标志的对象是null。虽然这本身可以用来看看应用程序是否开放,但似乎有点混乱。 Android中是否有一种方法可以从系统级别以某种方式检查应用程序当前是否已打开,以及用户是否正在查看应用程序?请记住,应用程序可以在技术上运行,但不可见,因为用户最近按下“主页”按钮将其发送到后台。

@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
         }
    }
}

4 个答案:

答案 0 :(得分:17)

我会使用订单广播来做到这一点。

onMessage方法中:

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

在您的活动中:

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();
    }
}

另一个BroadcastReceiver

public class SecondReceiver extends BroadcastReceiver {

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

}

清单:

<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>

基本上在onMessage方法中,您发送一个Intent,该Intent首先由YourActivity内注册的BroadcastReceiver接收,如果它正在运行且在前台,则由SecondReceiver接收。

答案 1 :(得分:2)

使用SharedPreferences保存布尔值isVisible,当您从首选项中获取值时,可以添加默认值。

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

答案 2 :(得分:1)

我一直在做的是对当前活动的引用。

我将每个onResume中的当前Activity设置为this,并在每个onPause中将其设置为null。

如果当前的Activity为null,则表示应用程序未打开。如果它不为null,您可以查看是否打开了正确的Activity并将其传递给该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();
    }

答案 3 :(得分:0)

对我有用的东西:

在其中创建最终的Class Constants,创建静态变量:

public final class Constants{
public static AppCompatActivity mCurrentActivity;
}

现在,在您的活动的每个简历上说:

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

接收通知时,检查当前活动是否为空,如果其null,应用程序未打开,如果activity不为null,则可以检查以下内容:

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