I tried using the sample code in this link but it seems outdated and it did not work. So what changes do I have to make and to what files to have my app start automatically when Android finishes booting up?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
The Sean's solution didn't work for me initially (Android 4.2.2). I had to add a dummy activity to the same Android project and run the activity manually on the device at least once. Then the Sean's solution started to work and the BroadcastReceiver was notified after subsequent reboots.
Listen for the ACTION_BOOT_COMPLETE and do what you need to from there. There is a code snippet here.
Update:
Original link on answer is down, so based on the comments, here it is linked code, because no one would ever miss the code when the links are down.
In AndroidManifest.xml (application-part):
...
...
Source: https://web.archive.org/web/20150520124552/http://www.androidsnippets.com/autostart-an-application-at-bootup
I would like to add one point in this question which I was facing for couple of days. I tried all the answers but those were not working for me. If you are using android version 5.1 please change these settings.
If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.
settings> app > your app > Restrict to launch (dis-select)
Another approach is to use
android.intent.action.USER_PRESENT
instead ofandroid.intent.action.BOOT_COMPLETED
to avoid slow downs during the boot process. But this is onlytrue
if the user has enabled the lock Screen - otherwise this intent is never broadcasted.Reference blog - The Problem With Android’s ACTION_USER_PRESENT Intent
Additionally you can use an app like AutoStart if you dont want to modify the code, to launch an android application at startup: AutoStart - No root
This is how to make an activity start running after android device reboot:
Insert this code in your
AndroidManifest.xml
file, within the<application>
element (not within the<activity>
element):Then create a new class
yourActivityRunOnStartup
(matching theandroid:name
specified for the<receiver>
element in the manifest):Note: The call
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
is important because the activity is launched from a context outside the activity. Without this, the activity will not start.Also, the values
android:enabled
,android:exported
andandroid:permission
in the<receiver>
tag do not seem mandatory. The app receives the event without these values. See the example here.