I am developing a small android application in which I wanted to start some service once the device boot is completed.
This is the code I used to try to accomplish this, but it not receiving boot complete event, so it isn't working.
//in manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
//my schedule receiver
public class MyScheduleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
Log.i("*************************TEST", "Service loaded at start");
Toast.makeText(context, "device started", Toast.LENGTH_SHORT).show();
}
}
}
MyScheduleReceiver never gets triggered; am I doing something wrong?
You miss a dot before MyScheduleReceiver
<receiver android:name=".MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Related
I found many topic but no case fit for my question.
The first my app working smooth, it is always startup with device by code bellow:
In AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and receiver:
<!-- start when module start broadcast -->
<receiver android:name=".StartOnBoot"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- listen messenger received -->
<receiver
android:name=".Phone.SmsListener"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And class StartOnBoot
public class StartOnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent serviceIntent = new Intent(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
} }
And now I added 2 broadcast to listen state of SD card and SIM card. But when I finish, My app can not start when my device restarted. And I don't know, where is problem, nothing clue.
<!-- broad cast SDCard receive-->
<receiver android:name=".Peripheral.SDCardListener">
<intent-filter>
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
<receiver android:name=".Phone.SimCardReceiver">
<intent-filter>
<action android:name="android.intent.action.SIM_STATE_CHANGED"/>
</intent-filter>
</receiver>
And 2 classes receiver:
public class SDCardListener extends BroadcastReceiver {
//restart app when SD card mounted
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG","SDcard state : " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)){
//sdcard eject
}else if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
SDCard.getInstance().SDCardMounted();
}
}
}
for sim state:
public class SimCardReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "Sim state :" + intent.getAction());
}
}
My device target is only Android 7.1, SDK lv 25.
Some methods I tried but not work:
I removed SimCardReceiver
I added "android:priortity to StartBoot" like this:
<intent-filter android:priority="1000">
But still not working, Remember, my app always work without 2 broadcast SDCardListener and SimCardReceiver
Extra question:
How many broadcast are available to use in one app. How many is good ? one or not limit
Could I merge all receiver to one broadcast
my program extends From BroadCastReciever to set Alarm, after the device is reboot receiver is not called, this is my code
1- BroadcastReceiver
public class myReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()!=null) {
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
Log.d("myActivity","repooted");
// alarm settings
}
}
}
2-xml
<receiver
android:name=".sync.myReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
In your AndroidManifest.xml have you added permission to receive boot complete?
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
I would like to build a boot receiver.
Manifest.XML
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And this is my Receiver Class:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("-->", "BOOT COMPLETED");
}
}
If i restart my device i get the message (on my device):
the app xxx was closed
Try to change this in your .manifest
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Also Try this out in your Reciever
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "Receiver Called", Toast.LENGTH_LONG).show();
}
}
Also Add android:persistent="true" in to your manifest tag
//AndroidManifest.xml file code
//permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"
// receiver
<receiver android:name="app.EasyLogger.receivers.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
//BroadCast Receiver class
public class BootReceiver extends BroadcastReceiver {
public static final String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context foContext, Intent intent) {
Log.d(TAG, "onReceive:BootCompletedReceiver called");
}
}
// log file not printed or also same for toast message
Yes, they have a whitelist, you have to be in the whitelist to receive BOOT_COMPLETED events.
I am using broadcast receivers for performing some action when the device is switched off and switched on again,but they are not working.These are the receivers in manifest file:
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
<receiver android:name=".RestartReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
These are the corresponding classes:
public class ShutdownReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("In","Switched Off");
}
}
public class RestartReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
SecureMessagesActivity.ToDoOnMobileSwitchOn();
Log.d("In","Switched On");
}
}
Please help me.Thanks in advance.
Be sure to request the RECEIVE_BOOT_COMPLETED permission in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>