简体   繁体   English

android:Alarmmannager强制停止应用程序

[英]android: Alarmmannager force stop the application

I'm using alarmmanager class to schedule notification for the user to lunch every day in a certain time but when th time comes it force stop the application this is the setAlarm() method that i call onCreate of the main activity 我正在使用alarmmanager类为用户安排在特定时间每天午餐的通知,但是当该时间到来时强制停止应用程序,这是我在主要活动的onCreate上调用的setAlarm()方法

    public void setAlarm() {
    // TODO Auto-generated method stub
    AlarmManager alarmMgr;
    PendingIntent alarmIntent;

    alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    // Set the alarm to start at approximately 8:00 p.m.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 10);
    calendar.set(Calendar.MINUTE, 17);
    // With setInexactRepeating(), you have to use one of the AlarmManager
    // interval
    // constants--in this case, AlarmManager.INTERVAL_DAY.
    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            alarmIntent);
}

and that's the the BroadcastReceiver Class 那就是BroadcastReceiver类

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    creatNotification(context, "Times Up", "5 Seconds Passed", "Alert");
}

private void creatNotification(Context context, String MSG, String MSgText, String MSGAlert) {
    PendingIntent NotifIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context);
    notifBuilder.setContentTitle("title").setContentText("Content")
            .setTicker("Ticker").setSmallIcon(R.drawable.ic_launcher);
    notifBuilder.setContentIntent(NotifIntent);
    notifBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    notifBuilder.setAutoCancel(true);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notifBuilder.build());
}

any Ideas ?? 有任何想法吗 ??

您是否在清单文件上定义了广播接收器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM