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 в манифесте и начать службы и должен использовать службу для составления уведомления –

+0

В манифесте я уже зарегистрировал получателя, но ничего .. –

+0

затем запустите сервис для запуска уведомления от ресивера –

ответ

0

ресивер класса

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

зарегистрировать Reciver и услуги в манифесте

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

     </receiver> 
<service android:name="service" > 
     </service> 
Смежные вопросы