Start a service when phone reboot completed - android

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

Related

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.

how to check whether boot receiver is working?

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

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

Application calling with screen lock

I want to launch an application immediately after the screen gets unlocked and at the time of booting. How is it possible? What changes do I need to make?
To launch your application when the screen locks, you need to register a BroadcastReceiver for the Intent.ACTION_SCREEN_OFF Intent for more information check the example http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
To launch your application at the time of boot you need a broadcast receiver that listens for the BOOT_COMPLETED action, please refer How to start an Application on startup?
First, you need the permission in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in your manifest, 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:enabled="true"
android:exported="true"
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 ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent("com.myapp.MySystemService");
context.startService(serviceIntent);
}
}
}
And now your service should be running when the phone starts up.

Categories

Resources