Answer the question
In order to leave comments, you need to log in
AlarmManager and Service or Receiver?
I searched all day, but I didn’t find anything (what I found helped only partially).
The bottom line is this, I have an AlarmManeger as it is already clear to everyone, it works perfectly without exaggeration, it is implemented with the help of BroadcastReceiver. Here is the actual code:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
public static String ONE_TIME = "onetime";
public static final String NOTIFICATION_TITLE = "notification_title";
public static final String NOTIFICATION_DETAILS = "notification_details";
public static final String NOTIFICATION_REQUEST = "notification_request_code";
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.something.alarm");
//Acquire the lock
wl.acquire();
//You can do the processing here update the widget/remote views.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
String title = extras.getString(NOTIFICATION_TITLE);
String details = extras.getString(NOTIFICATION_DETAILS);
int reqCode = extras.getInt(NOTIFICATION_REQUEST);
msgStr.append("One time Timer : ");
Utils.generateNotification(context, title, details, reqCode);
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));
Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
//Release the lock
wl.release();
}
public void CancelAlarm(Context context, int requestCode)
{
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, requestCode, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
public void setOnetimeTimer(Context context, Calendar d, String title, String details, int requestCode){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
intent.putExtra(NOTIFICATION_TITLE, title);
intent.putExtra(NOTIFICATION_DETAILS, details);
intent.putExtra(NOTIFICATION_REQUEST, requestCode);
Log.i("setOnetimeTimer", " "+requestCode);
PendingIntent pi = PendingIntent.getBroadcast(context, requestCode, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.YEAR, d.get(Calendar.YEAR));
calendar.set(Calendar.MONTH, d.get(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH, d.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, d.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, d.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, d.get(Calendar.AM_PM));
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
}
}
Answer the question
In order to leave comments, you need to log in
And androidManifest.xml tried to prescribe BroadcastReceiver ?
developer.android.com/intl/ru/guide/topics/manifes...
habrahabr.ru/post/149875
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question