Save Alarm after app killing

余生颓废 提交于 2020-01-07 03:08:15

问题


Code that starts AlarmManager.

        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);

    PendingIntent pi = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Its work fine, but when I kill my app in task killer, I lost my alarm. How to solve this problem?

Here full code: 1. Its alarm reseiver:

    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 SetAlarm(Context context)
//    {
//        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
//        intent.putExtra(ONE_TIME, Boolean.FALSE);
//        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//        //After after 30 seconds
//        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi);
//    }

    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){
        Log.i("st = ", title);
        Log.i("sd = ", details);
        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, PendingIntent.FLAG_UPDATE_CURRENT);

        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));
//
//        Log.i("TimeFragment", "" + calendar.get(Calendar.HOUR_OF_DAY));
//        Log.i("TimeFragment", "" + calendar.get(Calendar.HOUR));
//        Log.i("TimeFragment", "" + calendar.get(Calendar.AM_PM));
//        Log.i("QQQQQ", "Millis  = =  " + calendar.getTimeInMillis());
//        Log.i("QQQQQ","MillisІ = =  "+System.currentTimeMillis());

        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
    }
}

2. Its Utils:

public class Utils {

    public static NotificationManager mManager;
    public static void generateNotification(Context context, String title, String details, int reqCode){
        mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(context, StartPageActivity.class);
        Log.i("TITLEu = ", title);
        Log.i("DETAILsu = ", details);
        Log.i("Utils req ", "" + reqCode);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context,reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationCompat.Builder(context)
                .setTicker(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(title)
                .setContentText(details)
                .setContentIntent(pendingNotificationIntent)
//              .setVibrate()
//              .setSound()
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setAutoCancel(true).build();
        mManager.notify(0, notification);
    }
}

How to get around this problem?

来源:https://stackoverflow.com/questions/34240755/save-alarm-after-app-killing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!