我有一個切換開關,當點擊一個包含電池電量的通知時。這是註冊廣播接收器的服務。當切換處於false
狀態時,通知消失。那麼,即使狀態是假的,如果我啓動設備的通知,所以服務,啓動。我不明白的問題,但我我在logcat中看到,當我關掉撥動它會顯示這個錯誤:服務在啓動時不應該啓動
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線是:
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
當你與你的服務/活動完成,不再需要'ACTION_BATTERY_CHANGED'廣播,你應該叫'unregisterReceiver(mBatInfoReceiver)' – ozbek
在'onDestroy'還是在哪裏? –
我寫了'unregisterReceiver(mBatInfoReceiver);'in'onDestroy' ..沒有更多的錯誤,但啓動後通知再次顯示! –