gpt4 book ai didi

Android BroadCast 接收器和 GMC

转载 作者:行者123 更新时间:2023-11-30 03:41:27 24 4
gpt4 key购买 nike

我有这样的问题use registerReceiver for non activity and non service class .我尝试使用 Google Message Cloud,我需要从一个 Activity 中注册 BroadCastReceiver 并从另一个 Activity 中注销它。为此,我将 BroadcastReceiver 放在 Activity 之外的另一个包中。
在我的 Activity 中:

    public void onPushStateButtonClicked(View view) {
// controllo se il bottone è su on
boolean on = ((ToggleButton) view).isChecked();
PushClientService p = new PushClientService();
if (on) {
// se il bottone in impostazioni è settato ad on registro il dispositivo
p.customReceiver(this);
p.pushService(this);
}else if(!on) {
// se il bottone in impostazioni è settato ad off cancello il dispositivo

try{
GCMRegistrar.unregister(this);
p.customUnregisterReceiver(this);

}catch(IllegalArgumentException iAE){
Log.e("Errore:","device non registrato "+iAE);
}
}
}

在另一个类(class)我有这个:

private final BroadcastReceiver mHandleMessageReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
Log.d("Message",newMessage);
}
};
public void customReceiver(Context context){
context.registerReceiver(mHandleMessageReceiver,
new IntentFilter(DISPLAY_MESSAGE_ACTION));
}
public void customUnregisterReceiver(Context context){
context.unregisterReceiver(mHandleMessageReceiver);
}

这是 list :

?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testpushcall"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission android:name="com.example.testpushcall.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.testpushcall.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testpushcall.TestPush"
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="com.example.testpushcall.utils.CustomGCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.testpushcall" />
</intent-filter>
</receiver>
<service android:name="com.example.testpushcall.utils.GCMIntentService" />
</application>

</manifest>

当我取消注册接收者时,mHandleMessageReceiver 的值与注册的不同,我该怎么办?使 mHandleMessageReceiver 静态化?

请给我一个想法谢谢

最佳答案

我只使用 list 中声明的​​接收器解决了这个问题。并从谷歌服务器注册和取消注册 id

关于Android BroadCast 接收器和 GMC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15640411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com