I have in my manifest 2 recievers on BOOT COMPLETED. one is for location and the other is a generic one which wakes up my service activity . But I noticed when the boot is completed it is not called.
I added //if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {} but it did not help
In manifest I have it as follows:
<receiver android:name=".receiver.MyBootUpReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".helper.MyLocationReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and My recievers are as follows:
public class MyBootUpReceiver extends BroadcastReceiver {
Context ctx;
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"Receiver called..");
}
}
one BroadcaseReceiver is enough, you don't need two separate receivers for same event
add android:enabled="true" to your receiver in manifest file
<receiver
android:name="DeviceBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Related
I have specified all the actions in my manifest.xml file. I am trying to catch them in my Broadcast receiver onReceive but I'm unable to see them.
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
Toast.makeText(context,"Sample broadcast receiver boot completed",Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())){
Toast.makeText(context,"power connected",Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())){
Toast.makeText(context,"power disconnected",Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
In order to receive a BOOT_COMPLETED intent in your application you should add this permission to your AndroidManifest.xml :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
As described in Android SDK's documentation:
If you don't request this permission, you will not receive the broadcast at that time.
You can learn more about that permission here: android.permission.RECEIVE_BOOT_COMPLETED
My implementation of BootBroadcastReceiver
Permission taken in manifest :<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Intent filter in manifest for BootBroadcastReceiver :
<receiver android:name=".receivers.BootBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE"/>
</intent-filter>
</receiver>
The additional actions are result of some threads read online.
Below is implementation class :
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Application boot event");
Toast.makeText(context, "Boot completed", Toast.LENGTH_SHORT).show();
}
}
But somehow on reboot or power on I am not able to get this boot complete I added the toast also. No toast shown.
Also some people suggested make application install on internal memory only because the boot complete broadcast is not delivered to apps on external storage. By setting flag in manifest android:installLocation="internalOnly" I also did this but not worked for me. What might be wrong ?
Devices used for testing :
Moto E3 power (Android 6.0 ), Lenovo A6020a40 ( Android 5.1.1)
use this code
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
Toast.makeText(context, "Boot completed", Toast.LENGTH_SHORT).show();
}
}
}
also define in manifest file receiver like this
<receiver android:name="com.example.startuptest.StartUpBootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Hello I am new android developer. I create a sample project that works well when boot is completed.
Then I again create my main project to use this but boot completed not executed... I try my best to find problem.. I register this receiver through Manifest file not pragmatically...
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "boot completed", Toast.LENGTH_LONG).show();
//throw new UnsupportedOperationException("Not yet implemented");
}
and is Manifest XML
<receiver
android:name="com.azmizryk.mobilethefttracker.BootCompleted"
android:enabled="true"
android:exported="true" >
</receiver>
Add following Intent actions in manifest.xml file.
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
i.e. in your manifest file,change
<receiver
android:name="com.azmizryk.mobilethefttracker.BootCompleted"
android:enabled="true"
android:exported="true" >
</receiver>
to
<receiver
android:name="com.azmizryk.mobilethefttracker.BootCompleted"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
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
I am trying to register a Broadcast Receiver to receive broadcast events for the package events. Following is the code and my receiver in the manifest file. The log statment never happens, but I can clearly see the same broadcast firing for "HomeLoaders" (the Launcher) debug statements. What am I missing?
public class IntentListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("INTENT LISTNER:", intent.getAction());
}
}
<receiver android:name="IntentListener" android:enabled="true" android:exported="true">
<intent-filter>
<data android:scheme="package"></data>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.PACKAGE_CHANGED"></action>
</intent-filter>
</receiver>
It is possible that these Intents cannot be received by components registered in the manifest, but only by receivers registered in Java via registerReceiver().
These three intents namely,
Intent.ACTION_PACKAGE_ADDED
Intent.ACTION_PACKAGE_REMOVED
Intent.ACTION_PACKAGE_CHANGED
when broadcasted by the system, have
Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
flag added so that only the registered receivers will receive the broadcasts and no broadcast receiver components will be launched. Refer Intent and PackageManagerService class of source for further details.
This is my manifest, without
<category android:name="android.intent.category.DEFAULT" />
My app detects only the Android Market app install, but does not remove. Now it receives also the non-Android Market app broadcasts.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SomeActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.som.pakage.PackageInstallReceiver" >
<intent-filter >
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>