检查是否GCM的onMessage事件中的应用程序是开放的? [英] Check if app is open during a GCM onMessage event?

查看:139
本文介绍了检查是否GCM的onMessage事件中的应用程序是开放的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何检查,如果从GCM接收的onMessage()时,我的应用程序是开放的,目前对用户可见。起初,我只是用我自己的布尔可见性,但后来我意识到这是不可靠的,因为如果应用程序没有打开,该对象我用它来访问该标志是。虽然这本身就可以用来查看该应用程序是开放的,这似乎有点乱。有在机器人的方式从系统水平以某种方式检查该应用程序是否当前打开,并且如果用户正在观看应用程序?请记住,一个应用程序可以在技术上可以运行,但不可见,因为用户最近pressed家按钮将其发送到后台。

  @覆盖
保护无效的onMessage(背景为arg0,意图ARG1){
    字符串转= intent.getExtras()的getString(转)。
    如果(turn.equals(你){
         如果(/ *应用程序是开放的* /){< ------------------什么可以去这里?
             //不生成通知
             //显示游戏中的东西,而不是
         }
         其他{
             //生成通知,告诉玩家轮到自己
         }
    }
}
 

解决方案

我会用为了广播来做到这一点。

在你的的onMessage 方法:

 意图responseIntent =新的意向书(com.yourpackage.GOT_PUSH);
sendOrderedBroadcast(responseIntent,NULL);
 

在你的活动:

 公共类YourActivity延伸活动{

    最后的BroadcastReceiver mBroadcastReceiver =新的BroadcastReceiver(){

        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            //就在这里做你想要在你的活动
            abortBroadcast();
        }
    };

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        // .....
    }

    @覆盖
    保护无效的onPause(){
        unregisterReceiver(mBroadcastReceiver);
        super.onPause();
    }

    @覆盖
    保护无效onResume(){
        IntentFilter的过滤器=新的IntentFilter(com.yourpackage.GOT_PUSH);
        filter.setPriority(2);
        registerReceiver(mBroadcastReceiver,过滤器);
        super.onResume();
    }
}
 

其他的BroadcastReceiver

 公共类SecondReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        //在此接收器只是发送您的通知
    }

}
 

清单:

 <活动
    机器人:名称=。YourActivity
    机器人:标签=@字符串/ APP_NAME>
    <意向滤光器>
        <作用
            机器人:名称=android.intent.action.MAIN/>
        <类别
            机器人:名称=android.intent.category.LAUNCHER/>
    &所述; /意图滤光器>
< /活性GT;

<接收器
    机器人:名称=SecondReceiver。>
    <意图过滤器
        机器人:优先级=1>
        <作用
            机器人:名称=com.yourpackage.GOT_PUSH/>
    &所述; /意图滤光器>
< /接收器>
 

基本上在的onMessage 办法送你这是第一次收到由内而外 YourActivity 注册的BroadcastReceiver的意图,如果它正在运行,并在前台,否则就收到 SecondReceiver

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

解决方案

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.

这篇关于检查是否GCM的onMessage事件中的应用程序是开放的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆