应用程序在后台时的BroadcastReceiver [英] BroadcastReceiver when app is in background

查看:76
本文介绍了应用程序在后台时的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个应用程序,在该应用程序中,我会根据通过gcm推送通知发送的消息来对UI进行更改,并通过使用BroadcastReceiver onReceive函数来实现该操作,但只有在该应用程序位于前景,但是如果它在后台或关闭,则什么也没发生?

i am trying to write an app where i do changes to the UI depending on the msgs i send with gcm push notifications and i manged to do so by using BroadcastReceiver onReceive function to implement it but it only work if the app is in foreground but if it was in background or closed nothing happen so any way around that?

edit1:如果我理解您的问题对,则在清单文件中

edit1: in the manifest file if i understood ur question right

<receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="info.androidhive.gcm" />
            </intent-filter>
        </receiver>

        <service
            android:name="info.droiders.gcm.gcm.MyGcmPushReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

        <service
            android:name="info.droiders.gcm.gcm.GcmIntentService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>


   myBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                // notification received
                handleChanges(intent);
            }
        }
    };

推荐答案

如果您将广播接收者声明为应用程序中的活动或其他类的成员,则除非您的应用程序正在运行,否则它将不会运行.相反,您应该创建一个扩展广播接收器的独立类.因此,请更改此内容:

If you are declaring your broadcast reciever as an member of your activity or other class inside your app, then it won't run unless your app is running. Instead, you should create a stand-alone class that extends Broadcast receiver. So change this:

   myBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
            // notification received
            handleChanges(intent);
        }
    }
};

为此,并将其放置在其自己的文件中:

to this and place it in its own file:

public class GcmReceiver extends BroadcastReceiver {
     public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
            // notification received
            handleChanges(intent);
        }
    }
}

现在,即使您的应用未运行,Android也可以找到该类并实例化它.

Now Android can find the class and instantiate it even when your app isn't running.

编辑:已更正的类名称,以匹配在OP中显示的清单文件中声明的接收者名称.

Corrected class name to match the receiver name declared in the manifest file shown in OP.

这篇关于应用程序在后台时的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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