how to check whether boot receiver is working? - android

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

Related

Starting the app on device boot is not working well [duplicate]

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

run an application when reboot in Android

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.

How do I set my service's priority to ensure that it starts at bootup?

I'm trying to start my service at bootup, but I keep getting the 1245-1263/? I/ActivityManager﹕ Waited long enough for: ServiceRecord{3d8aa3b0 u9 myPackage/.myService} exception.
I've looked at this: My service always getting Waited long enough for: ServiceRecord error in Kitkat
which told me that the ActivityManager is putting starting my service on hold. I've confirmed through log statements that my receiver is indeed working. Here is the code in case anyone is interested:
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("OnStartUp", "Service is about to be started");
Intent myIntent = new Intent(context, myService.class);
context.startService(myIntent);
}
}
So, how can I set my service's priority to "essential" so that Android makes sure it starts? I looked at this: How to set the priority of android service? but it deals with restarting a service rather than the initial startup.
Here's the relevant bit of my manifest:
<receiver android:name=".PhoneStateReceiver"
android:enabled="true"
android:label="PhoneStateReceiver"
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>
<service
android:name=".myService"
android:enabled="true"
android:exported="true" >
</service>
Thanks in advance.
You have to register a bootup-completed broadcast receiver and from there start your service. Here is a working implementation. Perhaps its the <uses-permission> part you're missing?
EDIT
In my manifest (important parts) I have that <uses-permission> part at the beginning.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<!-- for services (must be rescheduled after boot) -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".services.BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</manifest>
My receiver class looks like this. Note, I'm starting an IntentService.
public class BootCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent msgIntent = new Intent(context, ReminderService.class);
msgIntent.setAction(ReminderService.ACTION_RESCHEDULE);
context.startService(msgIntent);
}
}
From the code that is posted, it looks like the manifest is looking for a service called .myService and in the BroadcastREceiver you're trying to start a service called LocationService.class.
This could be the issue.

Start a service when phone reboot completed

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

Is service is started automatically

I am new to android .Is service in android is started automatically when the mobile is switch on??if yes that's great.if no can any one explain how can i start the particular service ??
No, service is not started automatically after device boot. but you can register an android.intent.action.BOOT_COMPLETED for Starting Service when Device boot complete as:
AndroidManifest.xml :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<receiver android:name=".BootReceiver" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
BootReceiver.java :
public class BootReceiver extends IntentReceiver
{
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
public void onReceiveIntent(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION))
{
context.startService(new Intent(context,YourService.class));
}
}
}
In user based application service does not start automatically
you need to add below code
<receiver android:name="com.wallpaper.StartReceiver" xmlns:android="#unknown">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
you better read something on Broadcast receiver from here
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
A broadcast receiver is an Android component which allows to register for system or application events(in your case the ACTION_BOOT_COMPLETED)
As soon as the Android loads up it broadcast a message that the boot is completed and all the applications that are registered to receivce that event will receive it and you can do your stuff...
It can be done using this code below by adding it to the manifest file
<receiver android:name="some_pagacakge_name" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
here are some links for it
http://www.grokkingandroid.com/android-tutorial-broadcastreceiver/
you can start your service startService(intent)
To run it on Phone restart
add following permission in manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and declared a Broadcast Receiver like
<receiver android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
and then
public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";
#Override public void onReceive(Context context,Intent intent){
try{
context.startService(new Intent(context,yourServiceName.class));
Log.i(TAG,"Starting Service yourServiceName");
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}
Its depends on your requirement what kind of service you want to start and when you want to start that service. if you want to start particular service on startup then you have to register the reciever as Devangi Desai has mentioned and then you need to issue the startService() method.
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (intentAction.equals(Intent.ACTION_BOOT_COMPLETED))
context.startService(new Intent(context,
ConnectionService.class));
}
}
Here ConnectionService.class is class which extends a service and has the implementation of service.
They are not started automatically unless you explicitly define in the manifest that they should be started at boot time. To do this, you need to add the action
<action android:name="android.intent.action.BOOT_COMPLETED" />
in your manifest file for your service.
example:
<receiver android:name="MyStartupIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</receiver>
Please read this official guide for more detailed information about services:
https://developer.android.com/guide/components/services.html

Categories

Resources