2016-02-09 130 views
0

我使用alarmmanager類來安排用戶每天在某個時間吃午飯的通知,但是當時間到了時,它會強制停止應用程序 這是setAlarm()方法,我調用的主要活動android:Alarmmannager強制停止應用程序

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

的的onCreate,這就是該廣播接收器類

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

任何想法?

+2

任何stacktrace? –

回答

0

您是否在Manifest文件上定義了您的廣播接收器?

+0

I did ............. –

+0

我刪除了'calendar.setTimeInMillis(System.currentTimeMillis())',並使用'alarmMgr.setRepeating'而不是'alarmMgr.setInexactRepeating',並且與我。請嘗試。 –

+0

它甚至使用InexactRepeating,謝謝你kbeer;) –

相關問題