Android广播接收器未在应用程序关闭时执行

use*_*032 6 android intentfilter broadcastreceiver

我有一个Android应用程序,我计划在将来使用警报管理器执行事件(位置更新).只要应用程序在前台或后台运行,预定事件就会按预期执行.但是一旦我强制关闭任务管理器下的应用程序,或者当应用程序处于后台时由于内存问题导致android系统杀死应用程序时,我不再能够从警报管理器接收广播.

正如各种帖子和博客所建议我尝试使用1)Intent.Flag_Include_Stopped_Packages 2)接收器android:process =":remote"in manifest 3)接收器android:exported ="true"in manifest

在服务中:

Intent locationIntent = new Intent("com.dummy.intent");  
locationIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
locationIntent.putExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO, locationInfo);
context.sendBroadcast(locationIntent, "android.permission.ACCESS_FINE_LOCATION");
Run Code Online (Sandbox Code Playgroud)

在清单中:

<receiver android:name=".NearestStationBroadcastReceiver" android:enabled="true"
  android:exported="true" 
  android:process=":remote">
    <intent-filter>
        <action android:name="com.dummy.intent" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

有人可以帮帮我吗?

Com*_*are 5

但是一旦我强制关闭任务管理器下的应用程序,或者当应用程序处于后台时由于内存问题导致android系统杀死应用程序时,我不再能够从警报管理器接收广播.

这些都没有任何关系,所以如果你一直在模拟"当android系统因内存问题而杀死应用程序时"使用Force Stop,这就是你的问题.通过"设置"强制停止的应用程序会删除其警报等.模拟正在终止的进程的更好方法是从DDMS终止它.

1)Intent.Flag_Include_Stopped_Packages 2)清单中的接收器android:process =":remote"3)清单中的接收器android:exported ="true"

这些都与您的问题无关,并且android:exported="true"(和您使用的<intent-filter>)会引发安全问题,因为现在任何人都可以BroadcastReceiver出于任何原因随时运行您的问题.

这是一个成功处理警报事件的示例应用程序,即使在DDMS终止该进程之后也是如此.