1

我有這樣的問題use registerReceiver for non activity and non service class。我嘗試使用Google Message Cloud,並且需要從一項活動註冊BroadCastReceiver,並從另一項活動中取消註冊。爲此,我將BroadcastReceiver放置在另一個包中,而不是活動。
在活動中,我這樣的:Android BroadCast Receiver和GMC

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

中的其他類,我這樣的:

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

,這是清單:

?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靜態?

請給我一個想法TNKS

回答

1

我一直只使用接收器解決了這個問題在清單中聲明。 並從谷歌服務器註冊並取消註冊ID

相關問題