报警管理器和广播接收器不工作

时间:2016-04-20 01:49:15

标签: android

我的警报管理员课程

公共课PSMScheduler {

public static void setSchedule(Context context) {
    setSchedule(context,0,0,5);
    setSchedule(context, 18, 30, 0);
}

private static void setSchedule(Context context, int hour, int min, int sec) {

    // the request code distinguish different stress meter schedule instances
    int requestCode = hour * 10000 + min * 100 + sec;
    Intent intent = new Intent(context, EMAAlarmReceiver.class);

    PendingIntent pi = PendingIntent.getBroadcast(context, requestCode, intent,
            PendingIntent.FLAG_CANCEL_CURRENT); //set pending intent to call EMAAlarmReceiver.

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, min);
    calendar.set(Calendar.SECOND, sec);

    if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
        calendar.add(Calendar.DATE, 1);
    }

    //set repeating alarm, and pass the pending intent,
    //so that the broadcast is sent everytime the alarm
    // is triggered
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);
}

}

我的广播接收器

public class EMAAlarmReceiver extends BroadcastReceiver {
    @Override
public void onReceive(final Context context, Intent intent) {
    startPSM(context);
}

private void startPSM(Context context) {
    Intent emaIntent = new Intent(context, MainActivity.class); 
    emaIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(emaIntent);
}
}

我的主要活动

public PSMScheduler scheduler = new PSMScheduler();
public EMAAlarmReceiver receiver = new EMAAlarmReceiver();
Ringtone r;
Vibrator v;
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scheduler.setSchedule(this);

我不确定为什么这不起作用。我也在清单中声明了接收器。任何想法或建议将不胜感激。

0 个答案:

没有答案