我正在尝试编写一个应用程序,在该应用程序中,我会根据我通过gcm推送通知发送的消息进行UI更改,并通过使用BroadcastReceiver onReceive函数来实现该操作,但只有在应用程序位于前台时,是在后台还是关闭,什么也没发生,所以可以解决吗?
edit1:在清单文件中,如果我理解您的问题正确
<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);
}
}
};
如果您将广播接收者声明为应用程序中的活动或其他类的成员,那么除非您的应用程序正在运行,否则它将不会运行。相反,您应该创建一个扩展广播接收器的独立类。所以改变这个:
myBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// notification received
handleChanges(intent);
}
}
};
并将其放置在自己的文件中:
public class GcmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// notification received
handleChanges(intent);
}
}
}
现在,即使您的应用未运行,Android也可以找到该类并实例化它。
编辑:更正的类名称,以匹配在OP中显示的清单文件中声明的接收者名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句