Android 启动完成后自动启动应用程序
我想制作一个在其设置中具有自动启动选项的应用程序。我在我的应用程序中进行了从 PreferenceActivity 派生的设置活动,并为自动启动选项提供了 CheckBoxPreference。如果启用自动启动选项,我的应用程序应该在手机启动完成时启动。如果自动启动选项被禁用,那么它不应在启动完成时启动。
为了实现这一目标,我实现了 BroadcastReceiver 的派生类,它接收 BOOT_COMPLETED 意图,在 AndroidManifest.xml 中声明接收器,并在 AndroidManifest.xml 中授予权限。
在应用程序中还有一个 Application 的派生类并从应用程序派生类的 onCreate 方法启动服务。 如果我在 AndroidManifest.xml 中声明接收器,则在启动完成后调用我的应用程序的 onCreate,然后调用 BroadcastReceiver 的 onReceive 方法。
现在的问题是,无论自动启动是启用还是禁用,我的应用程序都会在每次启动完成时启动。禁用自动启动时是否可以不启动应用程序?
I want to make an application which has auto start option in its settings. I have made Settings activity in my application which is derived from PreferenceActivity and give CheckBoxPreference for auto start option. If auto start option is enabled my application should start when booting of phone is completed. And if auto start option is disabled then it should not start on boot completed.
To achieve this I have implemented derived class of BroadcastReceiver which receives BOOT_COMPLETED intent, declare receiver in AndroidManifest.xml and also give permission in AndroidManifest.xml.
In application also there is a derived class of Application and start service also from the onCreate method of application derived class. If I declare receiver in AndroidManifest.xml then after booting completed onCreate of my application called and after that onReceive method of BroadcastReceiver called.
Now the problem is that my application starts on boot completed every time whether auto start is enabled or disabled. Is it possible to not start application when auto start is disabled ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用共享首选项为
isAutoStartEnabled
存储一个布尔值,并在 BroadcastReciver 中检查该值,仅在为 true 时才触发 Intent。就您而言,问题不在于您是否收到广播,而在于谁收到广播。祝你好运..
我希望它有帮助..
You can use the shared preference to store a Boolean value for
isAutoStartEnabled
, and check this value in the BroadcastReciver, fire an intent only if it's true.In your case, the problem is not whether you receive the broadcast but who receives the broadcast. Best of luck..
I hope it helps..
我认为从Android 3.1起开始,接收
BOOT_COMPLETED
意图的BroadcastReceiver将无法工作。用户必须通过与其交互来唤醒应用程序。因此,启动设备后,所有第三方应用程序都会停止。
对于当前您的应用程序,您可以使用
SharedPreferences
自动启动您的应用程序。更新:(仅适用于低于 3.1 的 Android 版本,对于更高版本,它可以工作,但您在设备上启动完成后,用户必须与应用程序进行交互)
您需要使用具有
android.intent.action.BOOT_COMPLETED
意图的BroadcastReceiver
。将以下内容添加到您的清单文件中:
实现 BoradcastReciever 的 App_Receiver 类。实现
onReceive()
方法并从您的应用启动您最喜欢的活动。I think from Android 3.1 onwards your BroadcastReceiver which receives
BOOT_COMPLETED
intent its not going to work. User have to in-wake the application by interacted with it.So, After booting the device all third party application are lying as a stop.
And for currently your application you can use
SharedPreferences
for Auto-Start your application..UPDATE: (Only for Android version below 3.1 for higher version it works but you have to user interaction with your application after boot completed on device)
You need to use a
BroadcastReceiver
withandroid.intent.action.BOOT_COMPLETED
intent.Add following to your manifest file:
App_Receiver class implementing BoradcastReciever. Implement the
onReceive()
method and start your favorite activity from your app.您必须在清单中添加使用权限
android.permission.RECEIVE_BOOT_COMPLETED
。You have to add the uses-permission
android.permission.RECEIVE_BOOT_COMPLETED
in your Manifest.我希望这对你有帮助
I hope this helps you
以下代码对我有用:
The following code works for me: