我有一个切换,当点击一个电池电量开始的通知。这是一种注册广播接收器的服务。当切换处于false
状态时,通知消失。好吧,即使状态是假的,如果我启动设备通知,那么服务,启动。我无法理解这个问题,但我在logCat中看到,当我关闭切换时,它会显示此错误:
12-18 14:15:03.149: E/ActivityThread(337): Service com.dd.androreboot.NotificationService has leaked IntentReceiver com.dd.androreboot.NotificationService$1@44eecef0 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 com.dd.androreboot.NotificationService$1@44eecef0 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));
答案 0 :(得分:0)
服务: 您应该在onDestroy()
中取消注册接收器@Override
protected void onDestroy() {
unregisterReceiver(mBatInfoReceiver);
}
活动: 在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);
}