广播接收器和暂停活动

发布于 2024-12-11 23:52:16 字数 196 浏览 0 评论 0原文

我在活动中以编程方式注册了一个广播接收器。它响应 PACKAGE_REMOVED 意图,该意图在包被删除时触发。

问题是,它没有收到此消息。我认为这是因为当我离开该活动并移动到另一个活动以卸载应用程序时会触发意图,因此原始活动已暂停。 是否暂停的活动(接收器未在 onPause 中取消注册)也会暂停接收器?

I have a broadcast receiver registered programatically in a activity. It responds to the PACKAGE_REMOVED intent, that fires when a package gets removed.

The problem is, it doesn't get this message. I think this is due to that the intent is fired when I leave the activity and move to another activity to uninstall a app, so the original activity is paused.
Could it be that a paused activity (where the receiver is not unregistered in onPause) also pauses the receiver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱世争霸 2024-12-18 23:52:16

当您在活动中以编程方式注册广播接收器时,当活动暂停时它将不会收到广播。 BroadcastReceiver docs 在这一点上并不那么清楚。他们建议在 onPause 上注销只是为了减少系统开销。

如果您希望即使 Activity 不在前台也能接收事件,请使用 接收器 元素。

When you register a broadcast receiver programatically in an activity, it will NOT get broadcasts when the activity is paused. The BroadcastReceiver docs are not as clear as they could be on this point. They recommend unregistering on onPause solely to reduce system overhead.

If you want to receive events even when your activity is not in the foreground, register the receiver in your manifest using the receiver element.

牵你手 2024-12-18 23:52:16

Receiver 添加到您的项目中,您甚至无需启动即可获得此事件您的申请。

public class TestReciver extends BroadcastReceiver  {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TestReciver",intent.getAction()+"\n"
                +intent.getDataString()+"\n"
                +"UID: "+intent.getIntExtra(Intent.EXTRA_UID,0)+"\n"
                +"DATA_REMOVED: "+intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false)+"\n"
                +"REPLACING: "+intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
            );
    }

}

并在您的清单中添加如下内容(在您的标签内):

<receiver android:name="TestReciver" >
    <intent-filter >
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

当您使用这样的接收器时,您不会调用任何注册或取消注册,因此它将始终准备好获取数据。

请注意,如果您让用户将您的应用程序移至 SD 卡,则此操作将不起作用。如果在卸载 SD 卡时发送事件,则将无法访问接收器,并且您将错过该事件。

Add a Receiver to your project and you will get this event without even starting your application.

public class TestReciver extends BroadcastReceiver  {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TestReciver",intent.getAction()+"\n"
                +intent.getDataString()+"\n"
                +"UID: "+intent.getIntExtra(Intent.EXTRA_UID,0)+"\n"
                +"DATA_REMOVED: "+intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false)+"\n"
                +"REPLACING: "+intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
            );
    }

}

and in your manifest add it like this (Inside your <application> tag):

<receiver android:name="TestReciver" >
    <intent-filter >
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

When you use a receiver like this you do not call any register or unregister so it will always be ready to get data.

A note is that this will not work if you let the users move your app to the SD card. If an event is sent when the SD card is unmounted the receiver will not be accessible and you will miss the event.

如梦 2024-12-18 23:52:16

也许您可以在服务中注册接收器,该接收器将在后台运行

Maybe you can register the receiver in service which will run background

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文