android - 滑动清除应用程序时广播接收器停止触发

标签 android android-studio broadcastreceiver

Broadcast receiver 在从最近的列表中清除(滑动)应用后停止触发“onReceive(Context context,Intent intent)”方法,如何解决这个问题?

list 代码

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


    }
}

最佳答案

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

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

     }
    }

关于android - 滑动清除应用程序时广播接收器停止触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888888/

相关文章:

android - 删除忽略的 Android Studio(或 Intellij)更新构建

android - SMS Broadcast Receiver 收不到短信

android - 读取传入消息,直到读取验证短信

android - Kotlin 中 Dagger 2 的未解决引用

php - 解密在android中加密的php文件

android - android中的基本代码阅读

java - android studio 执行失败,因为 idk-1.8 的任务进程异常

intellij-idea - Android Studio : How to attach javadoc

java - 如何存储数据结构匹配.proto

android - PowerManager.PARTIAL_WAKE_LOCK android