如何告诉应用程序它是从设备启动时启动的?
您好,我正在为 Android 编写一个从启动时启动的应用程序,我想知道是否有一种方法可以告诉应用程序它是从设备启动时启动的?如果应用程序是手动启动的(即不是在设备启动时),我需要它做一些不同的事情。我正在使用 BroadcastReceiver 在设备启动时启动应用程序。
Hi I'm writing a application for android that is started from boot up and i wondered if there was a way of telling the application it was started from the boot up of the device? i need it to do something different if the application was manual started (i.e not when the device was started). i am using a BroadcastReceiver to start the application when the device starts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以制作两个不同的广播接收器,其中一个具有
ACTION_BOOT_COMPLETED
用于意图过滤器,另一个具有您将使用的其他意图过滤器。
或者创建一个具有两个 Intent 过滤器的广播接收器,例如:
然后在
onReceiver
中执行:编辑:
上面假设您在两种情况下使用
BroadcastReceiver
,但从您的问题来看可能并非如此。因此,如果您要启动一个
Activity
(或服务),那么在BroadcastReceiver
代码中,您可以执行以下操作:然后在该活动中,您可以执行以下操作:
如果我需要添加什么。
You could either make two different broadcast receivers one that has
ACTION_BOOT_COMPLETED
for the intent filter, and another that has the other intent filter that you would use.Or create one broadcastreceiver that has two intent filters like:
and then in the
onReceiver
do:EDIT:
The above assumes that you use the
BroadcastReceiver
under two circumstances, which may not be the case judging from your question.So if you are starting an
Activity
(or service), then in theBroadcastReceiver
code, you could do:Then in the activity, you could do:
Let me know if I need to add anything.
是的,在清单中将
broadcastreceiver
与 onbootcompleted
意图挂钩,当设备启动时,该接收器将被触发,您可以在那里做任何您想做的事情yes, hook a
broadcastreceiver
with a onboot completed
intent in the manifest and when the device boots up that receiver will be fired and you can do whatever you want there