2013-12-18 3 views
0

토글을 클릭하면 배터리 수준의 알림이 시작됩니다. 브로드 캐스트 리시버를 등록하는 서비스입니다. 토글이 false 상태 일 때 알림이 사라집니다. 음, 장치를 부팅하면 상태가 false 일지라도 알림이 표시되므로 서비스가 시작됩니다. 나는이 문제를 이해할 수 없다하지만 난 내가 토글을 해제 할 때이 오류가 표시되는 로그 캣에서 본 :부팅시 서비스가 시작되지 않아야합니다.

12-18 14:15:03.149: E/ActivityThread(337): Service com.dd.androreboot.NotificationService has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
12-18 14:15:03.149: E/ActivityThread(337): android.app.IntentReceiverLeaked: Service com.dd.androreboot.NotificationService has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:805) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:606) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1430) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1410) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1404) 
12-18 14:15:03.149: E/ActivityThread(337): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:467) 
12-18 14:15:03.149: E/ActivityThread(337): at com.dd.androreboot.NotificationService.onCreate(NotificationService.java:105) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2572) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.ActivityThread.access$1800(ActivityThread.java:135) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
12-18 14:15:03.149: E/ActivityThread(337): at android.os.Handler.dispatchMessage(Handler.java:102) 
12-18 14:15:03.149: E/ActivityThread(337): at android.os.Looper.loop(Looper.java:136) 
12-18 14:15:03.149: E/ActivityThread(337): at android.app.ActivityThread.main(ActivityThread.java:5017) 
12-18 14:15:03.149: E/ActivityThread(337): at java.lang.reflect.Method.invokeNative(Native Method) 
12-18 14:15:03.149: E/ActivityThread(337): at java.lang.reflect.Method.invoke(Method.java:515) 
12-18 14:15:03.149: E/ActivityThread(337): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
12-18 14:15:03.149: E/ActivityThread(337): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
12-18 14:15:03.149: E/ActivityThread(337): at dalvik.system.NativeStart.main(Native Method) 

문제가 될 수 있습니까?

public class NotificationService extends Service { 
    public static final int FM_NOTIFICATION_ID = 1; 
    public static int SHOW_TEMP = 1; // whether to show temperature in status bar 
    public static int SHOW_HEALTH = 1; // whether to show battery health in status bar 
    public static int SHOW_VOLTAGE = 1; // whether to show voltage in status bar 
    public static int SHOW_VOLTAGE_MILLIVOLT = 1; // whether to show millivolts in status bar 
    public static int SHOW_STATUS = 1; // whether to show Charging/Not Charging, etc 
    public static int SHOW_PERIODIC_TOASTS = 10; // whether to show periodic toast messages with charge level 

    @Override 
    public IBinder onBind(Intent arg0) { 
      return null; 
    } 

    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){ 
     @SuppressWarnings({ "unused" }) 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      int level = intent.getIntExtra("level", 0); 
      int temp = intent.getIntExtra("temperature", 0)/10; 
      int voltage = intent.getIntExtra("voltage", 0); 
      int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN); 
      int scale = intent.getIntExtra("scale", -1); 
      int health = intent.getIntExtra("health", -1); 
      boolean isPresent = intent.getBooleanExtra("present", false); 
      String strStatus; 
      String strHealth = "Health: "; 

      // Calculate level 
      if (level >= 0 && scale > 0) { 
       level = (level * 100)/scale; 
      } 

      if(SHOW_PERIODIC_TOASTS == 10){ 
       Toast.makeText(context, String.valueOf(level + "%"), Toast.LENGTH_SHORT).show(); 
      } 

      // Determine battery status   
      switch(status){ 
       case BatteryManager.BATTERY_STATUS_CHARGING : strStatus = getResources().getString(R.string.charging); break; 
       case BatteryManager.BATTERY_STATUS_DISCHARGING : strStatus = getResources().getString(R.string.discharging); break; 
       case BatteryManager.BATTERY_STATUS_NOT_CHARGING : strStatus = getResources().getString(R.string.discharging); break; 
       case BatteryManager.BATTERY_STATUS_FULL : strStatus = "Full"; break; 
       default : strStatus = "Status Unknown"; 
      } 


      // Determine battery health 
      switch(health){ 
       case BatteryManager.BATTERY_HEALTH_COLD : strHealth += "Cold"; break; 
       case BatteryManager.BATTERY_HEALTH_DEAD : strHealth += "Dead"; break; 
       case BatteryManager.BATTERY_HEALTH_GOOD : strHealth += "Good"; break; 
       case BatteryManager.BATTERY_HEALTH_OVERHEAT : strHealth += "Overheat"; break; 
       case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE : strHealth += "Over Voltage"; break; 
       case BatteryManager.BATTERY_HEALTH_UNKNOWN : strHealth += "Unknown"; break; 
       case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE : strHealth += "Unspc Failure"; break; 
       default : strStatus = "Health Unknown"; 
      } 

      // The initial call out notification 
      String NotificationTicket = getResources().getString(R.string.notifistart); 

      // The status user sees upon pulling notification bar down 
      String NotificationTitle = level + "%"; 
      if(SHOW_STATUS == 1){ NotificationTitle += " "+ getResources().getString(R.string.and) +" " + strStatus; } 

      // The content show underneath the battery percentage 
      String NotificationContent = ""; 
      if(SHOW_TEMP == 1) { NotificationContent += temp + "°C "; } 
      if(SHOW_VOLTAGE == 1) { 
       if(SHOW_VOLTAGE_MILLIVOLT == 1){ 
        NotificationContent += voltage + "mV "; 
       } else { 
        NotificationContent += voltage/1000 + "V "; 
       } 
      } 
      if(SHOW_HEALTH == 1) { NotificationContent += strHealth; } 

      Bitmap largeIcon = (Bitmap)BitmapFactory.decodeResource(context.getResources(), R.drawable.notification); 

      addNotification(NotificationTitle, NotificationContent, NotificationTicket, largeIcon, level); 
     } 
    }; 

    @Override 
    public void onCreate() { 
      super.onCreate(); 
      this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
    } 

    @Override 
    public void onDestroy() {   
      Toast.makeText(this, getResources().getString(R.string.notifistop), Toast.LENGTH_SHORT).show(); 
      removeNotification(); 
      super.onDestroy(); 
    } 

    // Start notification 
    private void addNotification(String title, String body, String ticker, Bitmap largeIcon, int iconLevel) { 
     NotificationCompat.Builder builder = 
       new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.levellist, iconLevel) 
       .setLargeIcon(largeIcon) 
       .setContentTitle(title) 
       .setContentText(body) 
       .setOngoing(true) 
       .setNumber(iconLevel) 
       .setTicker(ticker); 

     Intent notificationIntent = new Intent(this, MainNavDrawer.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     builder.setContentIntent(contentIntent); 

     // Add as notification 
     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.notify(FM_NOTIFICATION_ID, builder.build()); 
    } 

    // Remove notification 
    private void removeNotification() { 
     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.cancel(FM_NOTIFICATION_ID); 
    } 
} 

105 라인은 다음과 같습니다 :이 서비스 당신이들의 OnDestroy에서 리시버를 등록 해제해야 () 활동에 대한

@Override 
    protected void onDestroy() { 
    unregisterReceiver(mBatInfoReceiver); 
    } 

:

this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
+1

하고 더 이상 필요'ACTION_BATTERY_CHANGED' 방송, 당신은'unregisterReceiver (mBatInfoReceiver)를 호출해야한다' – ozbek

+0

등록을 취소한다 'onDestroy' 또는 정확히 어디에서? –

+0

'onDestroy'에'unregisterReceiver (mBatInfoReceiver);'를 썼습니다. 더 이상의 오류는 없지만 부팅 후 알림이 다시 나타납니다! –

답변

0

서비스의 경우 : 수신자를 onResume()에 등록하십시오.

@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
    } 

당신은 onpause에서 리시버()는 서비스/활동 완료

@Override 
    protected void onPause() { 
    unregisterReceiver(mBatInfoReceiver); 
    } 
+0

onPause에서 왜 onDestroy 메서드가 아닌가요? –

+0

내 대답 –

+0

업데이트 어떻게 서비스에서'mBatInfoReceiver'를 활동으로 호출 할 수 있습니까? –

관련 문제