Some of my app users are complaining that my app is killing their batterys but im having a hard time figuring out the issue. Can anyone spot where I might be going wrong?
https://play.google.com/store/apps/details?id=com.walkingroutes.beta01
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent callingIntent) {
long interval = 86400000;//set interval 86400000 = 24 hours
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long timeOrLengthofWait = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
Intent intent = new Intent(context, UpdaterService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, -1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), interval, pendingIntent);
}
}
Related
I want to run a task every ten minute, and i used AlarmManager like following:
public static void startAlarmOnce(Context context, String action, long triggerAtMillis) {
Intent intent = new Intent(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
}
But, there's one problem: if someone registered a BroadcastReceiver use the same action, then he can receive the broadcast.
so, how to keep the broadcast in my own app, like LocalBroadcast do?
Use an explicit intent:
public static void startAlarmOnce(Context context, String action, long triggerAtMillis) {
Intent intent = new Intent(context, YourBroadcastReceiver.class);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
}
Where YourBroadcastReceiver.class should be the name of the class you implemented as BroadcastReceiver.
Scenario :
My task will executed every midnight using AlarmManager, let's say at 00:00:00 and should be repeated everyday
i use the following code :
Calendar setCalendar = Calendar.getInstance();
setCalendar.set(Calendar.HOUR_OF_DAY, 0);
setCalendar.set(Calendar.MINUTE,0);
setCalendar.set(Calendar.SECOND,0);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, setCalendar.getTimeInMillis(), 1000 * 60 *60 *24, pi);
I also did this alarmManager.setInexactRepeating(AlarmManager.RTC, setCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); but the result still same (not execute every midnight)
The receiver :
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//execute my task
}
}
Android Manifest :
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name="com.projectx.activity.MainActivity$AlarmReceiver"/>
The code above didn't do anything even midnight has passed. Is there any wrong with my code? please help.
And also sometimes the alarm executed every several second after i set the alarm (not only at midnight)
I have no idea, what causes it.
try this:
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context, TestNotifyService.class);
PendingIntent alarmIntent = PendingIntent.getService(context, 0, intent1, 0);
long _Nalarm;
Calendar now = Calendar.getInstance();
Calendar wakeupcall = Calendar.getInstance();
wakeupcall.setTimeInMillis(System.currentTimeMillis());
wakeupcall.set(Calendar.HOUR_OF_DAY, 21);
wakeupcall.set(Calendar.MINUTE, 59);
if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
_Nalarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_Nalarm=wakeupcall.getTimeInMillis();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, _Nalarm,AlarmManager.INTERVAL_DAY, alarmIntent);
}
Try the below code
Calendar setCalendar = Calendar.getInstance();
setCalendar.set(Calendar.HOUR_OF_DAY, 0);
setCalendar.set(Calendar.MINUTE,0);
setCalendar.set(Calendar.SECOND,0);
setCalendar.add(Calendar.DATE, 1);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, setCalendar.getTimeInMillis(), 1000 * 60 *60 *24 , pi);
And the receiver:
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//execute my task
Toast.makeText(context, " hello", Toast.LENGTH_SHORT).show();
}
}
Let me know if any issues.
I'm testing AlarmManager to use in my app, and it is firing my Broadcast Receiver immediately when I want it to fire after 1 minute. The code is below:
public class SetMealTimersActivity extends Activity {
PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_meal_timers);
br = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Ready to Go!", Toast.LENGTH_LONG).show();
}
};
registerReceiver(br, new IntentFilter("com.ian.mealtimer"));
pi = PendingIntent.getBroadcast(this, 0, new Intent(
"com.ian.mealtimer"), 0);
am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
60 * 1000, pi );
}
try :
am.set(AlarmManager.RTC_WAKEUP,
Calendar.getInstance().getTimeInMillis()+60*1000, pendingIntent);
it is working for me.
If using an exact alarm, make sure it's time is in the future. Otherwise it will fire immediately.
Try changing SystemClock.elapsedRealtime() to System.currentTimeMillis() and AlarmManager.ELAPSED_REALTIME_WAKEUP to AlarmManager.RTC_WAKEUP.
Try to use AlarmManager.setExact(int, long, PendingIntent) if you use Android API > 18 or compile with API < 19, because the time management for this methods changed with API 19. Maybe that helps. Read the documentation for more information.
make id for pendingIntent as this
pendingIntent = PendingIntent.getActivity(this, 999123266,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
All example
public void setAlarm_sat(int dayOfWeek1) {
cal1.set(Calendar.DAY_OF_WEEK, dayOfWeek);
Intent intent = new Intent(this, RemmemberActivity.class);
pendingIntent = PendingIntent.getActivity(this, 999123266,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Long alarmTime = cal1.getTimeInMillis();
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
alarmTime,7*24 * 60 * 60 * 1000,
pendingIntent);
// am.set(AlarmManager.RTC, cal1.getTimeInMillis(), pendingIntent);
}
I have defined my Android alarm and BroadcastReceiver as follows. My hope was that I want the alarm to go off two minutes later and every 15 minutes subsequently. This does not seem to be happening. Why is this?
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 2);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(getApplicationContext(), DailyNotificationReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
public class DailyNotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("received", "received");
}
}
As discussed in the comments, the BroadcastReceiver was not registered.
Please register the BroadcastReceiver in the manifest
i tried to write a simple service for an android device. it looks like
public void onReceive(Context context, Intent intent) {
// do something
// new alarm
setNextAlarm(context);
}
public static void setNextAlarm(Context context) {
Intent myIntent = new Intent(context, MainReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// trigger
Calendar cal = Calendar.getInstance();
// here is possible that trigger time will be different by each call
long triggerTime = computeNextTriggerTime();
long triggerAtTime = cal.getTimeInMillis() + triggerTime;
alarmManager.setInexactRepeating(AlarmManager.RTC, triggerAtTime, triggerTime, pendingIntent);
}
Well, I noticed that it works, but sometimes it crashes after days or hours of using and I'm quite sure, that it is not because of the code, that do the logic and that I didn't post here.
The trigger time is between 10 and 30 minutes.
Every help will be appreciated :)