service not getting started at boot - android

i have a receiver that starts a service at boot but the receiver never gets fired when at boot
manifest
<service android:enabled="true" android:name=".BatteryService"></service>
<reciever android:name=".BatteryReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</reciever>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
why does it not work, everything looks correct
if i open my app the service starts fine
Receiver class
public class BatteryReciever extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
arg0.startService(new Intent(arg0, BatteryService.class));
}//end onRecieve
}

tyzyj,
It looks like you may have misspelled the word receive in multiple places as recieve.
Try...
<receiver android:name=".BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
unless the class name is misspelled as well? You may want to post the code for that Receiver class.

Related

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.

Android - Start a program immediately on startup of device

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 .. :)

broadcasting of BOOT_COMPLETED intent action does not work properly

I have a receiver class listening on several actions but it can not catch the android.intent.action.BOOT_COMPLETED action. What I am doing wrong? here is my manifest file:
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--<receiver android:name=".OtherReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>-->
<receiver android:name="com.myApp.AppReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="com.myApp.wifitimer"/>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" android:path="com.myApp" />
</intent-filter>
</receiver>
as it can be seen I added the permission again inside the receiver and the name of the receiver gets the full name of the class as this answer suggests.
here is the broadcast receiver class:
#Override
public void onReceive(Context arg0, Intent arg1) {
String action1 = arg1.getAction();
if(action1.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d("receiver","action is: boot");
}
if(action1.equals("android.intent.action.PACKAGE_REPLACED")) {
Log.d("receiver","action is: package");
}
}
When I run the app the receiver catches the android.intent.action.PACKAGE_REPLACED but when I restart the phone the receiver does not catch the BOOT_COMPLETED.
However when I comment in the .OtherReceiver in the Mainfest file it can catch it!
here is the code of this class:
public class OtherReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
String action = arg1.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d("new receiver","action is: boot");
}
}
}
just the same as the other one. So my question is why I need define a separate receiver for the BOOT_COMPLETED action?
Edit: I also tried to send the action via adb according to this, and without any permission I could catch it with the AppReceiver class:
am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.blubuk/.AppReciever
First, remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED" from your <receiver> element.
Second, your <data> portion of your <intent-filter> is applying to all <action> elements within that <intent-filter>, which you do not want. There is no Uri on ACTION_BOOT_COMPLETED.
However, rather than creating a separate <receiver> element, you could just create a separate <intent-filter> element on your original <receiver> element. Move your <action android:name="android.intent.action.BOOT_COMPLETED" /> to the new <intent-filter> (and perhaps that com.myApp.wifitimer one too), so that they are unaffected by the <data> of your first <intent-filter>.
Replace your Manifest with this and I am sure it will work. Placing android:permission in the receiver tag was incorrect.
<receiver
android:name="com.myApp.AppReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

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