N
N
Nick20152015-12-11 01:16:21
Android
Nick2015, 2015-12-11 01:16:21

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);
    }
}

As already said, everything works as it should, displays a message at the right time, then when I complete the process through the task manager, the messages are no longer shown. I know very well that it works in this way, how to make it so that after closing it continues its work? Wouldn't it be better to use the Service with START_STICKY (although I tried it and it didn't work for me :()?
I don't want to make the service last forever, only to have everything work after closing, since most people close their programs this way (this is not about now) so that the ability to close (or rather stop) from "started services" remained.
Please tell me I will be very grateful. If possible, links to solutions to this problem, if I searched badly, or better in my example, what to fix, etc.
Thank you for your understanding! !!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Kazantsev, 2015-12-11
@saintbyte

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 question

Ask a Question

731 491 924 answers to any question