0

为什么我的服务在 BOOT_COMPLETED 开始,但在 BATTERY_LOW 不开始?代码:

MyScheduleReceiver.java

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

服务.java

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

并表现出来

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-permission android:name="android.permission.BATTERY_STATS">
<receiver android:name="MyScheduleReceiver" >
     <intent-filter>
         <action android:name="android.intent.action.BATTERY_LOW" />
     </intent-filter>

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

那么我想要显示的电池电量不足的通知没有出现..如果我用 BOOT_COMPLETED 更改操作并且我重新启动设备工作..为什么?

4

1 回答 1

0

看看这个答案:https ://stackoverflow.com/a/12348778/2508174

看起来您也需要以编程方式订阅这些意图。顺便说一句,您似乎不需要 BATTERY_STATS 权限即可。

希望能帮助到你

于 2013-07-18T10:24:37.103 回答