0

BATTERY_LOW ではなく BOOT_COMPLETED でサービスが開始されるのはなぜですか? コード:

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 に答える