10

我想知道在收到onMessage()来自 GCM 的消息时如何检查我的应用程序是否已打开并且当前对用户可见。起初,我只是使用自己boolean isVisiblenull. 虽然这本身可以用来查看应用程序是否打开,但看起来有点混乱。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

4 回答 4

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

另一个广播接收器

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 的方法中,YourActivity如果它正在运行并且在前台,它首先由注册的 BroadcastReceiver 接收,否则它由SecondReceiver.

于 2013-04-11T13:12:36.880 回答
2

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

SharedPreferences settings = context.getSharedPreferences("NAME_XXX", Activity.MODE_PRIVATE);
settings.getBoolean("visible", false);
于 2013-04-11T11:10:20.443 回答
1

我总是做的是参考当前的活动。

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

如果当前 Activity 为 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
        }
    }
}

正确活动:

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

@Override
    protected void onPause() {
        synchronized (GCMIntentService.CURRENTACTIVITYLOCK) {
            GCMIntentService.currentActivity = null;
        }
        super.onPause();
    }
于 2013-04-11T12:47:27.700 回答
0

对我有用的东西:

创建一个最终的类常量,在其中创建静态变量:

public final class Constants{
public static AppCompatActivity mCurrentActivity;
}

现在,在你的活动简历上说:

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

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

if(Constants.mCurrentActivity instanceof MainActivity){
   ((MainActivity) Constants.mCurrentActivity).yourPublicMethodOrStaticObject;
}
于 2016-04-19T12:05:52.050 回答