在停止的应用程序中接收广播 [英] Receive broadcasts in stopped application

查看:90
本文介绍了在停止的应用程序中接收广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,处于停止状态(甚至无法运行一次)的应用程序无法接收广播。如果将标志 FLAG_INCLUDE_STOPPED_PACKAGES 添加到广播意图中,则有一种接收方法。

By default, an application which in stopped state (not yet run even once) could not receive broadcasts. There is a way to receive it if we add the flag FLAG_INCLUDE_STOPPED_PACKAGES to the broadcast intent.

我的问题是我需要接收系统意图 android.intent.action.MEDIA_MOUNTED (并且我不想添加该标志)。

My problem is that I need to receive system intent android.intent.action.MEDIA_MOUNTED (and I don't want to add that flag to it).

即使在已停止的应用程序中也如何接收?

How can I receive it even in stopped application?

在我看来,这是有可能的,因为例如 com.android.shell 应用程序能够接收这样的意图,即使它停止了。

It seems to me that for sure it's possible because for example com.android.shell application is able to receive such intents even if it's force stopped. How it does it?

编辑

只需澄清一下,因为它似乎并不明显:

Just to clarify, because it appeared that this is not obvious:

这里的所有内容都是从Android Open Source Project开发人员的角度来看的。我正在修改操作系统源代码,可以预安装要接收广播的应用程序,并使用平台证书对其进行签名,可以使用隐藏的API等。一切都允许,甚至可以修改系统。

Everything here is from the point of view of Android Open Source Project developer. I'm modifying the operating system source code and the application which I want to receive the broadcast could be preinstalled, signed with platform certificate, it can use hidden API, etc. Everything is permitted, even modifications of the system.

推荐答案

将应用程序声明为系统应用程序,即使您的应用程序处于 已停止状态,也应允许您接收常规广播 >。

Declaring your application as a system app should allow you to receive regular broadcasts even if your app is in "stopped state".

在清单中添加以下内容,并确保您的应用程序已由平台密钥签名:

Add the following to your manifest and make sure your application is signed by the platform key:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
...
android:sharedUserId="android.uid.system">

这篇关于在停止的应用程序中接收广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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