广播接收器停止触发

广播接收器停止触发

本文介绍了轻扫清除应用程序时,广播接收器停止触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

广播接收器从最近列表中清除(滑动)应用后停止触发"onReceive(Context context,Intent intent)"方法,该如何解决?

Broadcast receiver stop triggering "onReceive(Context context, Intent intent)" method after clear(swipe) app from recent list , how to solve this issue ?

清单代码

   <receiver
    android:name="*.receiver.TestReceiver"
    android:enabled="true"
    android:exported="true"
    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" />
        <action android:name="com.google.android.c2dm.intent.REGISTER" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />

        <action android:name="android.intent.action.USER_PRESENT" />
        <category android:name="pacakge" />
    </intent-filter>
</receiver>

接收方代码

public void onReceive(Context context, Intent intent) {
    LogUtils.LOGD("notfy",  "Receiver : ");
    if(intent!=null){
        Bundle extras = intent.getExtras();
        if(extras!=null) {
            try {
                String regId = extras.getString("registration_id");
                String message = extras.getString("message");
                String qsid = extras.getString("id","");
                String type = extras.getString("type","");
                // extras.getString("aps");


                       /*code*/



            } catch (Exception e){

                LogUtils.LOGD("notfy",  "onReceive last exception: " + e.getMessage());
            }
        }


    }
}

推荐答案

在主应用中启动/停止服务

In main app start/stop the service

Intent service = new Intent(context, MyService.class);
context.startService(service);
...
Intent service = new Intent(context, MyService.class);
context.stopService(service);

服务

  public class MyService extends Service
    {
     private static BroadcastReceiver m_Receiver;

     @Override
     public IBinder onBind(Intent arg0)
     {
      return null;
     }
    @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
    }

     @Override
     public void onCreate()
     {
      Receiver();
     }

     @Override
     public void onDestroy()
     {
     unregisterReceiver(m_Receiver);
     }

     private void Receiver()
     {
     m_Receiver = new BroadcastReceiver()
      {
       @Override
       public void onReceive(Context context, Intent intent)
       {
        //place all ur stuff here
       }
      }

     }
    }

这篇关于轻扫清除应用程序时,广播接收器停止触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:53