2015-11-27 44 views
-1

我想在android中創建一個定期通知,而且我幾乎已經實現了它,問題在於用我的代碼打開應用程序而不是創建通知,這裏是我的代碼:在android中設置定期通知

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle(this.getText(R.string.notificationMessage)) 
        .setContentText("") 
        .setAutoCancel(true); 

    Intent resultIntent = new Intent(this, MainActivity.class); 

    // Because clicking the notification opens a new ("special") activity, there's 
    // no need to create an artificial back stack. 
    PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        this, 
        0, 
        resultIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(1,mBuilder.build()); 


    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 07); 
    calendar.set(Calendar.MINUTE, 00); 
    calendar.set(Calendar.SECOND, 00); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, resultPendingIntent); //set repeating every 24 hours 

更新的代碼:

類主要活動:

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class), PendingIntent.FLAG_CANCEL_CURRENT); 

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 15); 
    calendar.set(Calendar.MINUTE, 20); 
    calendar.set(Calendar.SECOND, 00); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent); //set repeating every 24 hours 

類NotificationService:

public class NotificationService extends IntentService { 

/** 
* Creates an IntentService. Invoked by your subclass's constructor. 
* 
* @param name Used to name the worker thread, important only for debugging. 
*/ 
public NotificationService(String name) { 
    super(name); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 

    Log.d("NotificationService", "onHandleIntent"); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle(this.getText(R.string.notificationMessage)) 
        .setContentText("") 
        .setAutoCancel(true); 

    Intent resultIntent = new Intent(this, MainActivity.class); 

    // Because clicking the notification opens a new ("special") activity, there's 
    // no need to create an artificial back stack. 
    PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        this, 
        0, 
        resultIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(1,mBuilder.build()); 



} 

}

回答

1

您正在將活動的未決意圖傳遞給警報管理器。您需要將通知代碼移到服務中(我推薦使用IntentService)。

然後,您可能需要創建一個掛起的意圖,運行該服務並將該掛起的意圖傳遞到警報管理器。這種方式每次警報發生時,您的通知代碼就會運行。

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class)); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent); 
+0

我提出的所有通知代碼到一個新的類如你所說,並創建了的getService的的PendingIntent(它要求我一個標誌,所以我試圖用FLAG_CANCEL_CURRENT),但他並沒有叫新的類,我不知道爲什麼。 – Juan

+0

你能發佈更新後的代碼嗎? – csmurphy84

+0

我編輯了更新後的代碼 – Juan

0

PendingIntent點到你的活動。您只需使用PendingIntent.getService,並讓此服務顯示您的通知。