2016-06-08 125 views
0

我想在狀態欄上創建一個常量通知(如當前電池百分比 - 始終存在),但該圖標每天都會在00:00更改。每天更改android狀態欄通知

此外,我希望此通知將在重新啓動設備後自動發出警報,而不僅在用戶打開應用程序之後。

我在MainActivity中創建了通知,但我不知道如何在重新啓動後使其警報,並且我不知道如何在每天午夜更改它。

我試着創建AlarmService和廣播接收器,但它每5秒運行一次,它太多了,所以我刪除了它。

我當前的代碼是:

  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
       .setContentTitle(getString(R.string.notificationTitle)) 
       .setContentText(....) 
       .setLargeIcon(largeIconBitmap) 
       .setSmallIcon(getResources().getIdentifier("icon_status_" + myHelper.getCurrentImageNumber(),"drawable", getPackageName())); // change the icon at mid-night. 'getCurrentImageNumber' should get me the right icon number 

      Intent resultIntent = new Intent(this, MainActivity.class); 
      PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); 
      mBuilder.setContentIntent(resultPendingIntent); 
      Notification notification = mBuilder.build(); 
      notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 


      mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      mNotifyMgr.notify(mylHelper.myRequestCode, notification); 

現在我看到一個通知,但圖標沒有發生變化,因此在重新啓動後不報警。我在stackoverflow中看到了一些答案,但沒有任何幫助。

回答

1

我不會給你所有的代碼,你將需要通過做。

這裏是你應該做的:

1使用服務來啓動您的通知,並保持邏輯進行更新。如果通知已經存在,則可以更新現有通知或以編程方式將其解除並替換爲新通知。

2在AndroidManifest.xml中使用多個意圖過濾器註冊廣播接收器以啓動您的服務。

<receiver android:name=".myReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.TIME_TICK" /> 
    </intent-filter> 

    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

3創建擴展廣播接收器類並使用並啓動上述服務的類。

+0

我是否需要在AndroidManifest.xml中註冊我的服務?昨天這是什麼導致它每5秒運行一次 – TamarG

+0

是的,你也需要在這裏註冊它。您將有一個廣播接收器啓動服務,以響應接收上述兩個意圖過濾器(TIME_TICK,BOOT_COMPLETED)之一。您的廣播接收機應該具有邏輯,只在時間爲00:00或您的設備剛啓動時才啓動服務。發佈一些更多的代碼,如果你喜歡更多的建議! – apelsoczi