当应用程序完全关闭时,WakefulBroadcastReceiver不会触发(在android 6中)

时间:2017-05-03 11:38:24

标签: android google-cloud-messaging android-service android-wake-lock

我想在我的android项目中使用GCM。

我的代码是:

public class GcmReceiver extends WakefulBroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
      GcmMessageHandler.class.getName());

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));


    setResultCode(Activity.RESULT_OK);
  }
}

我的InstentService是:

public class GcmMessageHandler extends IntentService {

  private Handler handler;

  public GcmMessageHandler() {
    super("GcmMessageHandler");
  }

   @Override
   public void onCreate() {
     super.onCreate();
   }

  @Override
  protected void onHandleIntent(Intent intent) {
  Bundle extra = intent.getExtras();

  showToast();

  GcmReceiverOld.completeWakefulIntent(intent);
 }


  private void showToast()
  {
  handler.post(new Runnable() {
   @Override
   public void run() {
     Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
   }
    });
  }

}

我还添加了Wale_Lock权限以显示:

<uses-permission android:name="android.permission.WAKE_LOCK" />

并将此行添加到显示

<receiver android:name=".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="ir.test.gcmtester" />
                    </intent-filter>
        </receiver>

        <service android:name=".gcm.GcmMessageHandler"
                 android:exported="false"
            />

但我的问题是,当应用程序完全关闭时(在android 6中),GcmReceiver类不会触发。

如何解决这个问题?

0 个答案:

没有答案