Android Broadcast接收器未在应用程序关闭时执行 [英] Android Broadcast receiver not executed on application close

查看:95
本文介绍了Android Broadcast接收器未在应用程序关闭时执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,在该应用程序中,我安排了一个事件(位置更新),以后将使用警报管理器执行该事件。只要应用程序在前台或后台运行,计划的事件就会按预期执行。但是一旦我在任务管理器下强制关闭该应用程序,或者当应用程序在后台运行时由于内存问题而android系统终止了该应用程序,我将无法再从警报管理器接收广播。

I have an android application, where I schedule to an event (location update) to be executed in the future using Alarm manager. The scheduled event executes as expected as long as the application runs in the foreground or in the background. But once I force close the application under task manager or when android system kills the application due to memory issue when the app is at background, I am no longer able to receive the broadcast from the alarm manager.

如各种帖子和博客所建议,我尝试使用
1)Intent.Flag_Include_Stopped_Packages
2)清单
中的android:process =:remote 3)接收器清单中的android:exported = true

As suggested by various posts and blogs i tried using 1) Intent.Flag_Include_Stopped_Packages 2) receiver android:process=":remote" in manifest 3) receiver 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");

在清单中:

<receiver android:name=".NearestStationBroadcastReceiver" android:enabled="true"
  android:exported="true" 
  android:process=":remote">
    <intent-filter>
        <action android:name="com.dummy.intent" />
    </intent-filter>
</receiver>

有人可以帮帮我吗?

推荐答案


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

But once I force close the application under task manager or when android system kills the application due to memory issue when the app is at background, I am no longer able to receive the broadcast from the alarm manager.

这些彼此无关,因此,如果您一直在模拟 android系统通过使用强制停止杀死了由于内存问题导致的应用程序,这就是您的问题。已通过设置强制停止的应用已删除其警报,等等。模拟您的进程终止的更好方法是从DDMS终止它。

These have nothing to do with each other, so if you have been simulating "when android system kills the application due to memory issue" by using Force Stop, that is your problem. An app that has been force-stopped through Settings has its alarms removed, among other things. A better way to simulate your process being terminated is to terminate it from DDMS.


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

1) Intent.Flag_Include_Stopped_Packages 2) receiver android:process=":remote" in manifest 3) receiver android:exported="true" in manifest

这些都不与您的问题相关,和 android:exported = true (以及您使用< intent-filter> )引起的安全问题,因为现在任何人都可以出于任何原因使您的 BroadcastReceiver 随时运行。

None of those are related to your problem, and android:exported="true" (and your use of an <intent-filter>) raises security issues, as now anyone can cause your BroadcastReceiver to be run at any time, for any reason.

这里是一个示例应用程序,即使该过程已经完成,也可以成功处理警报事件已被DDMS终止。

Here is a sample application that successfully processes alarm events, even after the process has been terminated by DDMS.

这篇关于Android Broadcast接收器未在应用程序关闭时执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆