onReceive() function of BroadcastReceiver always called when device is rebooted - android

My program is implement BroadcastReceiver to setting Alarm. But I got issue : After setting time to alarm, I power off device. After that, I start device and onReceive() function of BroadcastReceiver always call. This is my code :
<receiver android:name="com.futaba.broadcastservice.AlarmBroadCastReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
How to fix this issue?

Have you read the documentation of BOOT_COMPLETED? It will run when the system is done booting.

Here's what you could do to reschedule your alarm after rebooting.
Code goes on your receiver.
public class AlarmBroadCastReceiver extends BroadcastReceiver {
Context ct;
#Override
public void onReceive(Context context, Intent intent)
{
ct=context;
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Intent contentIntent = new Intent(ct, AlarmBroadCastReceiver.class);
PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute);
AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent);
}
}
Just to be sure, you have to declare the uses-permission as follow
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="com.futaba.broadcastservice.AlarmBroadCastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

Related

AlarmReceiver not getting called with android:process=":remote"

I have implemented a service to run with the alarm manager.
I read this link: Should I use android: process =“:remote” in my receiver?
I thought this would be a nice feature for my app, since i want the service to keep running after my app is down.
But when i add this line of configuration to my receiver on the manifest, my service stops being called.
Any clues?
Here is my receiver declaration:
This works:
<receiver
android:name=".service.MyAlarmReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name=".service.MyAlarmReceiver"></action>
</intent-filter>
</receiver>
This wont'n work:
<receiver
android:name=".service.MyAlarmReceiver"
android:enabled="true"
android:process=":remote"
android:exported="false">
<intent-filter>
<action android:name=".service.MyAlarmReceiver"></action>
</intent-filter>
</receiver>
public class MyAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Time to start scan service!");
Intent intent = new Intent(context, BeaconFinderService.class);
context.startService(intent);
}
}
This is how i start my alarm manager:
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), MyAlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.BLE_SERVICE_LOOP_TIME, pIntent);
When you configure your service to run in remote mode (android:process=":remote"), you will have to debug the process :remote instead as usual .
Personal bug:
So I was having an exception when trying to access FirebaseUser on the service. When in remote mode, you can't access the FirebaseUser, since your process runs on another context.
I had to pass the user through intent extras when initializing the service.
That was all!

Alarmmanager not working after phone reboot

I had created alarmmanager on button click. but it not working after phone reboot. my AlarmbroadcastReceiver not call on phone reboot. it work when phone lock , application killed but not work after phone reboot i had created one progressbar which start on button click and stop after alarm broadcast fired but it not stop when phone reboot. i had added my button click event and broadcast receiver class
Button click event
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pb1.setVisibility(View.VISIBLE);
progress_edit.putBoolean("progress_one", true);
progress_edit.apply();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intnt = new Intent(getApplicationContext(), AlarmbroadcastReceiver.class);
intnt.setAction("com.ex.Alarm");
PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending);
Log.d("Broadcast ","Fired");
}
});
BroadcastReceiver Class
#Override
public void onReceive(Context context, Intent intent) {
Log.d("inside","broadcast receive");
if(intent.getAction().equalsIgnoreCase("com.ex.Alarm"))
{
enterSys_progress_edit.putBoolean("progress_one", false);
enterSys_progress_edit.apply();
Toast.makeText(context,"Receive",Toast.LENGTH_LONG).show();
}
}
My Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.krutarth.alarm">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmbroadcastReceiver" 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>
</application>
</manifest>
all alarms are reset when phone restart ,So create a callback on reboot like this
public class BootCompletedIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
////// reset your alrarms here
}
}
}
ALSO ADD THIS TO YOUR MANIFEST TO REGISTER THE BROADCAST
<receiver android:name=".receiver.BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Create a method say,
setMyalarm(){
// here set the alarm as u need
}
now call this method setMyAlarm ,whenever and wherever u need to set that particular alarm,whether its on a button click or whether it on a reboot reciever
prefect ans by Ak9637
but didn't forgot to add permisttion of
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Android——How can i know whether the app is autostart or not

I want to know whether the user allows autostart permissions of the app. I have already tried this:
ComponentName mComponentName = new ComponentName(getPackageName(),".AutoStartReceiver");
int a = getPackageManager().getComponentEnabledSetting(mComponentName);
the autostart permission can be granted and denied by security app,but I don't know how to get the status in my application.
such as the picture
you are probably using this permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
After that, you can implement a BroadcastReceiver:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}
And just add the class to your manifest-file:
<receiver android:enabled="true" android:name=".BootUpReceiver"
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>
To check directly, if you have Autostart permissions, use this:
int p = ContextCompat.checkSelfPermission(Activity.this, Manifest.permission.RECEIVE_BOOT_COMPLETED);
if (p == PackageManager.PERMISSION_GRANTED) {
//Yay, you have the receive boot completed (= Autostart) permission!
}
To get a list of receivers, registered for a specific intent, you can use
Use PackageManager and queryBroadcastReceivers().
To get a list for BOOT_COMPLETE, construct an intent with BOOT_COMPLETE action

BroadcastReceiver is not called by alarm

There are a lot of related questions and I beleive I've read them all twice or so. For some reason I seem to have tomatoes on my eyes. I do not see the error.
I've got an alarm set up using Alarm manager. (Happy to share more code if required.)
private static String ALARM_ACTION = "de.klecker.BigBen.Alarm";
private static PendingIntent createPendingIntent() {
Intent alarmIntent = new Intent(getContext(), BigBenAlarm.class);
alarmIntent.setAction(ALARM_ACTION);
return PendingIntent.getBroadcast(getContext(), 0, alarmIntent, 0);
}
getContext() returns a reference to the application.
public static void setAlarmFromNow() {
AlarmManager manager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
PendingIntent intent = createPendingIntent();
// First cancel any ongoing alarm
manager.cancel(intent);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5 * 1000), intent);
Log.d("BigBen", "Alarm is set!");
}
The alarm is properly registered. This is an extract of the output of
adb shell dumpsys alarm
Batch{2f6ac936 num=1 start=68344020 end=68344020}:
RTC_WAKEUP #0: Alarm{f8a5037 type 0 when 1453976393037 de.klecker.bigben}
tag=*walarm*:de.klecker.BigBen.Alarm
type=0 whenElapsed=+4s155ms when=2016-01-28 11:19:53
window=-1 repeatInterval=0 count=0
operation=PendingIntent{3df19a4: PendingIntentRecord{18a6130d de.klecker.bigben broadcastIntent}}
And it got actually fired. This is an extract from the log:
01-28 11:19:53.038 V/AlarmManager( 885): sending alarm {18a6130d type 0 *walarm*:de.klecker.BigBen.Alarm}
01-28 11:19:53.039 V/AlarmManager( 885): done {18a6130d, *walarm*:de.klecker.BigBen.Alarm} [1ms]
But the receiver did not get called. This is the receiver class:
public class BigBenAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
Log.d("BigBen", "Alarm fired!!!");
}
}
Neither the log statement appears on the log, nor does the phone vibrate. (Virbration permission was declared in manifest). I tried toasts and playing sounds and setting a breakpoint, but apparently, the method is never called.
This is how the receiver is registered:
AndroidManifest.xml:
<receiver android:name="de.klecker.bigben.BigBenAlarm">
<intent-filter>
<action android:name="de.klecker.BigBen.Alarm"/>
</intent-filter>
</receiver>
So which important detail did I miss?
Thanks to everybody who tried hard.
And yes, I did have tomatoes on my eyes. Although quite embarassing I leave this in and provide an answer in case that anybody else runs into the same issue.
The big mistake was within the android manifest. The receivers need to be registered within the application tags.
AndroidManifest.xml:
<application
android:name=".BigBenApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Big Ben"
android:supportsRtl="true"
android:theme="#style/AppTheme"
>
<activity android:name=".BigBenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="de.klecker.bigben.BigBenAlarm"
android:enabled="true" >
<intent-filter>
<action android:name="de.klecker.BigBen.Alarm"/>
</intent-filter>
</receiver>
</application>
For some reason I closed the application and then tried to register the receiver.

Start AlarmManager if device is rebooted

In my app I want to run some code every day at a specific time using an AlarmManager. In the android documentation I found this:
Registered alarms are retained while the device is asleep [...] but will be cleared if it is turned off and rebooted.
And that is the problem. I want to run the code even if the user reboots the phone. If the user reboots the phone he currently has to relaunch my app to start alarms again. How can I prevent this? Is there a better mechanism I should use instead?
Create Boot Receiver using following code :
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context pContext, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
// Do your work related to alarm manager
}
}
}
In your Manifest, register this Broadcast receiver :
<receiver
android:name="com.yourapp.BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And don't forget to add permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Use can u create service using broadcast reciever on device boot up
<receiver android:enabled="true" android:name=".YourReceiver"
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
You will need to add a boot receiver in your manifest like this
<application ... >
<receiver android:name=".OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</intent-filter>
</receiver>
</application>
And then create the boot receiver class like this...
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctxt, Intent intent) {
AlarmHelper.setAlarm(ctxt);
}
}
My alarm helper class is a simple start of day alarm like this...
public class AlarmHelper {
public static void testAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.SECOND, 10);
setAlarm(context, when);
}
public static void setAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.DAY_OF_YEAR, 1);
when.set(Calendar.HOUR_OF_DAY, 0);
when.set(Calendar.MINUTE, 0);
when.set(Calendar.SECOND, 0);
setAlarm(context, when);
}
#SuppressLint("SimpleDateFormat")
private static void setAlarm(Context context, Calendar when) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
Boolean showNotifications = prefs.getBoolean("PREF_SHOW_NOTIFICATIONS",
false);
if (showNotifications) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, getPendingIntent(context.getApplicationContext()));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.i(TAG, "Alarm set " + sdf.format(when.getTime()));
}
}

Categories

Resources