I want to run an application automatically when the phone reboot. I have added these lines to my code:
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyActivity.class);
context.startService(serviceIntent);
}
}
And I have added these lines to my Manifest :
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and it's permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
It works properly when MyActivity class contains just some background tasks. For example when I want to turn on a LED. But when I want to play a media (ex. a video) it doesn't work. Just says service has started and doesn't play anything.
How can I fix it? Is there anything that I must add to the code?
The problem is not with your code! It's about android device.
When the device boot up, you receive the broadcast. But pay attention, when you receive that broadcast, the SD CARD is still populating and you can't access it. If that media is on SD card you cant play it.
Also, you should pay attention to something else: if the user install your app in his/her SD card, that broadcast receiver would never trigger.
So, if you want to prevent problem in some devices, in your manifest, choose internal for your install location too.
Do this change in BroadcastReceiver:
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyActivity.class);
// Change is here...
serviceInten.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(serviceIntent);
}
}
In the manifest:
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter android:priority="500">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
And leave permission as it is.
Related
I tried using the sample code in this tutorial 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?
First, you need the permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
}
And now your service should be running when the phone starts up.
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):
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:enabled="true"
android:exported="true"
android:name="yourpackage.yourActivityRunOnStartup"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Then create a new class yourActivityRunOnStartup (matching the android:name specified for the <receiver> element in the manifest):
package yourpackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class yourActivityRunOnStartup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
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 and android:permission in the <receiver> tag do not seem mandatory. The app receives the event without these values. See the example here.
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):
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class); //MyActivity can be anything which you want to start on bootup...
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Source: https://web.archive.org/web/20150520124552/http://www.androidsnippets.com/autostart-an-application-at-bootup
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
For Android 10 there is background restrictions.
For android 10 and all version of android follow this steps to start an app after a restart or turn on mobile
Add this two permission in Android Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Add this in your application tag
<receiver
android:name=".BootReciever"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Add this class to start activity when boot up
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, SplashActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}}
We need Draw overlay permission for android 10
so add this in your first activity
private fun requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + this.packageName)
)
startActivityForResult(intent, 232)
} else {
//Permission Granted-System will work
}
}
}
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.
For flutter user, you can create a file named MainActivityReceiver.kt in package folder. eg. android/app/src/main/kotlin/com/your_company/package.
MainActivityReceiver.kt:
package com.your_company.package
import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;
class MainActivityReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val i = Intent(context, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i)
}
}
}
Modify your AndroidManifest.xml file refer to the first answer.
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 of android.intent.action.BOOT_COMPLETED to avoid slow downs during the boot process. But this is only true 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
I created a boot receiver class:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("Boot receiver","Working")
}
}
My manifest file:
<receiver android:name="com.voonik.android.receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
I used the adb command - am broadcast -a android.intent.action.BOOT_COMPLETED
Still i cant be sure whether it is working.How can i test it?
I think you have missed the #Override for on receive.
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Boot receiver","Working")
}
in my manifest i have;
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
and;
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in BootUpReceiver.java i have;
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
You should just be able to see your Log in the logcat, try using System.out.println("BOOT RECEIVED");
instead of Log
Keep in mind that packages have different states (Major changes came from Android 3.1).
If the user is not opening the app by himself (at least once) than your package will not receive this intent.
Otherwise it will be really easy to run some malicious code without user interaction.
As you can imagine for security reasons.
There you can find more information: Trying to start a service on boot on Android
Unless you will place your APK in /system/apps folder and will sign it with system certificate.
P.S.: There is some flag that you can pass with the intent, might be interesting for you FLAG_INCLUDE_STOPPED_PACKAGES
I want to Start a service when phone reboot completed. I will install my app but will not launch it.
I have proceed with the following ways using Broadcast Receiver listening to BOOT_COMPLETED intent.
My Receiver is as follows...
public class MyReceiver extends BroadcastReceiver {
static final String TAG = "MyReceiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "******* OnReceive");
Log.d(TAG, "Boot Completed Caught");
context.startService(new Intent(context, MyService.class));
}
}
In Manifest I have added the following lines of code:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name="MyService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.logic.MyService" />
</intent-filter>
</service>
<receiver android:name="MyReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
But receiver is not listening once boot is completed. Remember I have only installed my app but never launch it.But my requirement is such that I should not launch my app after installing it in my device. Anybody can help me on this.
I googled it and found that as I have not launched my app it is in stopped state and boot completed intent is ignoring my receiver. The below links that might help you in understanding my issue:
http://devmaze.wordpress.com/2011/12/05/activating-applications/
the service and MyReceiver must be full packagename,
like this
Hello everyone and thanks for your trouble,
I have a program detailed here. This uses a microphone icon in order to start listening for voice. However, I want this program to run immediately on start up of the device. How is this possible? Also, I want the program to run without any visuals and simply return the text translation to my running program, which will take care of what command to execute. Any help on any of these topics? It will be greatly appreciated.
Yes you can do it. For this you'll need set permission to AndroidManifest.xml.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In your AndroidManifest.xml, define your service and listener for the BOOT_COMPLETED action:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Then you have to define the receiver that will get the BOOT_COMPLETED action and start your service.
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MySystemService.class);
context.startService(serviceIntent);
}
}
}
Hope this helps .. :)
I want to start my application automatically when the phone boots.
I declared a BroadcastReceiver in the manifest file.
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I made a java file for the receiver.
Autostart.java
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MushTouchActivity.class);
pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(pushIntent);
}
}
}
But, the application does not launch when I switch my phone on. Can anyone tell me what I am missing here ?
try your if statement like this:
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, MushTouchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
In case you are on Android 3.1 or newer:
Make sure that you started your application at least once manually (e.g. by opening it from the app drawer). Otherwise your app is marked as stopped by the system:
Applications are in a stopped state when they are first installed but are not yet launched
Stopped apps do not receive any broadcast intents, including BOOT_COMPLETED.
See Android 3.1. Platform - Launch controls on stopped applications for more information.
The best answer would be to show a Notification and ask the user to launch the app from that notification and use a pending intent for that activity in the notification.
Tested on Android 10
1. Create A Broadcast Listener
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == "android.intent.action.ACTION_SHUTDOWN") {
// Your tasks for shut down
} else {
// Your tasks for Boot up
}
}
}
2. Config Manifest
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:theme="#style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
</application>
If you search for a sample application code for working with services in background and control actions on screen off/on then visit this repo:
https://github.com/varunon9/DynamicWallpaper
you can update this repo source code with step 1 and 2 for control boot and shutdown events in addition to screen on/off.