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
Related
I'm trying to implement a receiver that reacts to Bluetooth devices being connected or disconnected. However, I only receive the broacasts when the application is open.
I've added the receiver to the manifest:
<receiver android:name=".BleReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>
And my receiver looks like this:
public class BleReceiver extends BroadcastReceiver {
private static final String TAG = "BleReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Got intent: " + intent.getAction());
}
}
This works just fine when the app is open, but if I eg. use the task switcher and swipe the activity, no broadcasts are received anymore.
The output of adb shell cmd package query-receivers --brief -a android.bluetooth.device.action.ACL_CONNECTED looks just fine:
Receiver #3:
priority=0 preferredOrder=0 match=0x108000 specificIndex=-1 isDefault=false
com.example.blelistenerapp/.service.ble.BleReceiver
Also, I checked the implicit broadcast exceptions, and these two actions are listed there.
Try
<receiver android:name=".BleReceiver">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
</receiver>
In an application, I am trying to implement a service which needs to run every time when device boot process completed.
Application unable to broadcast service only in MI devices.
Manifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
BroadcastReceiver
public class SavedLocationBroadcast extends WakefulBroadcastReceiver {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onReceive(Context context, Intent intent) {
App.showToast(context,"BROADCAST","SEND");
Intent service = new Intent(App.getContext(), MyLocationService.class);
startWakefulService(App.getContext(), service);
}}
Reciever in manifest
<receiver android:name="com.geolocation.broadcastreciever.SavedLocationBroadcast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="com.geolocation.app.ACTION_PULSE_SERVER_ALARM" />
</intent-filter>
</receiver>
Please share your suggestion is anything wrong in implementation.
This is solution for your bug.
What you need to do is that , you need to mention this permission also in your Intent-Filter.
<receiver android:name="com.geolocation.broadcastreciever.SavedLocationBroadcast"
android:enabled="true"
android:exported="true" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
Here you need to do one thing also.
Go to "Settings-Apps_open your app info-Manage permisson" in yourdevice
Here check all things here.
Because for some devices it will not work. Ex.Red mi
Try this !
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
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.
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.