0

我想在ACTION_BATTERY_LOW時創建一個簡單的通知..我想要的是當電池電量低出現時在狀態欄中啓動一個通知。我已經試過這樣: 在OnCreate如何在ACTION_BATTERY_LOW時在通知欄中創建通知?

this.registerReceiver(this.batteryLowNotifi, new IntentFilter(Intent.ACTION_BATTERY_LOW)); 

然後:

private void checkPref(){ 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
           TutorialNotification.this); 
       notificationBuilder.setContentTitle("Title"); 
       notificationBuilder.setContentText("Context"); 
       notificationBuilder.setTicker("TickerText"); 
       notificationBuilder.setWhen(System.currentTimeMillis()); 
       notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon); 

       Intent notificationIntent = new Intent(this, TutorialNotification.class); 
       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
           notificationIntent, 0); 

       notificationBuilder.setContentIntent(contentIntent); 

       notificationBuilder.setDefaults(Notification.DEFAULT_SOUND 
           | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); 

       mNotificationManager.notify(SIMPLE_NOTIFICATION_ID, 
           notificationBuilder.build()); 
    } 

    private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { 

     public void onReceive(Context context, Intent intent) { 
      checkPref(intent); 
     } 
    } 

但沒有發生。任何幫助?謝謝

+0

的reciver和服務你應該登記在清單中reciver和啓動服務,並應該使用該服務來撰寫通知 –

+0

在我的清單中已經註冊了接收器但沒有任何內容。 –

+0

然後啓動一個服務來啓動接收器的通知 –

回答

0

reciver類

public class MyScheduleReceiver extends BroadcastReceiver { 


    // Restart service every 30 min 
    private static final long REPEAT_TIME = 30*1000*4;//1800000 ; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
Intent service = new Intent(context, service.class); 
context.startService(service); 
}} 

服務類

public class service extends Service { 

    NotificationManager mNotificationManager; 
     @Override public IBinder onBind(Intent intent) { 
     // Not used 
     return null; 
     } 

     @Override public void onCreate() { 
     super.onCreate(); 
    mNotificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
checkPref(); 
} 
@Override 
     public void onDestroy() { 
     super.onDestroy(); 

     } 

private void checkPref(){ 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
           service.this); 
       notificationBuilder.setContentTitle("Title"); 
       notificationBuilder.setContentText("Context"); 
       notificationBuilder.setTicker("TickerText"); 
       notificationBuilder.setWhen(System.currentTimeMillis()); 
       notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon); 

       Intent notificationIntent = new Intent(this, service.class); 
       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
           notificationIntent, 0); 

       notificationBuilder.setContentIntent(contentIntent); 

       notificationBuilder.setDefaults(Notification.DEFAULT_SOUND 
           | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); 

       mNotificationManager.notify(1, 
           notificationBuilder.build()); 
    } } 

登記清單

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

     </receiver> 
<service android:name="service" > 
     </service>