安排某些任务无法正常进行-如何解决该问题?

时间:2018-10-20 12:08:52

标签: java android alarmmanager

我正在尝试使用android中的AlarmManager安排一些任务。这是我的代码。

设置警报代码:

 try {
     // cancel before set new alarm 
     cancelAllPrevious() ;       

     AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     Intent intent = new Intent(context,AlarmReciver.class);

     int calenderDay = some_day

     PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
     Log.w("MyAlarmManager",intent.getExtras().toString());

     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, hour);
     calendar.set(Calendar.MINUTE, minute);
     calendar.set(Calendar.DAY_OF_WEEK, calenderDay);


     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, alarmIntent);
} catch (Exception e) {
    e.printStackTrace();
}

取消上一个警报

public void cancelAllPrevious() {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent updateServiceIntent = new Intent(context,AlarmReciver.class);
    updateServiceIntent.putExtra("alarm_type","my_alarm");

    PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0);

    // Cancel alarms
    try {
        alarmManager.cancel(pendingUpdateIntent);
        Log.i("AlarmManager","My Alaram Cancelled");
    } catch (Exception e) {
        Log.e("Alarm Manager", "AlarmManager update was not canceled. " + e.toString());
    }
}

清单

<receiver
    android:name = "**.*****.****.AlarmReciver"
    android:enabled="true" />

AlarmReciver.java

public class AlarmReciver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

    Log.e("AlarmManger","method called ");

    }
}

调试值是->

enter image description here

它不起作用。请帮助我。

谢谢。

0 个答案:

没有答案