方向更改服务泄漏了最初在此处注册的IntentReceiver您是否缺少对unregisterReceiver的调用?

问题描述

一个服务已泄漏了IntentReceiver ,我阅读了所有其他问题,但找不到修复程序。我有一个绑定服务的活动(扩展了AppCompatActivity)。该服务扩展了抽象服务类,因为我希望能够根据某些条件创建特定的服务,并在抽象类中保留通用性。

public abstract class DeviceService extends Service {
...
public DeviceService() {
        super();
    }

    @Override
    public IBinder onBind(Intent intent)  {
     Log.e("DeviceService","I'm the service: "+this.toString());
     return _binder;
    }

    public class LocalBinder extends Binder {
        DeviceService getService() {
            return DeviceService.this;
        }
    }
...
}

所以服务是:

public class UsbService extends DeviceService implements Runnable{
...
public UsbService() {
        super();
    }
...
}

该活动在onCreate方法中将服务绑定

public class displayActivity extends AppCompatActivity {

    ...

    private DeviceService deviceService;    
    private boolean deviceServiceUpdateReceiverRegistered = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        Log.d(TAG,"Set up the toolbar.");
        Toolbar generalToolbar = (Toolbar) findViewById(R.id.app_toolbar);
        setSupportActionBar(generalToolbar);
        // Get a support ActionBar corresponding to this toolbar
        ActionBar ab = getSupportActionBar();
        if(ab!=null)
            ab.setdisplayShowTitleEnabled(true);
        Intent usbServiceIntent = new Intent(this,UsbService.class);
        bindService(usbServiceIntent,(ServiceConnection) deviceServiceConnection,BIND_AUTO_CREATE);
        
     }

在onResume方法注册broadcastreciver,并在onPause方法中取消注册

 @Override
    protected void onResume() {
        super.onResume();
        try {
            if(!deviceServiceUpdateReceiverRegistered) {
                Log.d(TAG,"onResume. Register the device service broadcaster receiver.");
                registerReceiver(deviceServiceUpdateReceiver,deviceServiceUpdateIntentFilter());
                deviceServiceUpdateReceiverRegistered = true;
                if (deviceService != null) {
                    try {
                        // connects to the device
                        Log.d(TAG,"onResume. Connect to the device.");
                        deviceService.connect(deviceName);
                    }
                    catch (Exception e) {
                        Log.e(TAG,"Error: "+e.getMessage());
                    }

                }
            }
        }
        catch (Exception e) {
            Log.e(TAG,"Could not register broadcast receiver. Error: "+e.getMessage());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            Log.d(TAG,"onPause. disconnect the device.");
            if(deviceService != null)
                deviceService.disconnect();
            Log.d(TAG,"onPause. Unregister the device service broadcaster receiver.");
            unregisterReceiver(deviceServiceUpdateReceiver);
            deviceServiceUpdateReceiverRegistered = false;
        }
        catch (Exception e) {
            Log.e(TAG,"Could not unregister broadcast receiver. Error: "+e.getMessage());
        }
    }

通过onDestroy方法取消服务

@Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if(deviceServiceUpdateReceiverRegistered) {
                Log.d(TAG,"onDestroy. Unregister the device service broadcaster receiver.");
                unregisterReceiver(deviceServiceUpdateReceiver);
                deviceServiceUpdateReceiverRegistered = false;
            }
            //deviceService.disconnect();
            Log.d(TAG,"onDestroy. Unbind the device service: "+deviceService.toString());
            unbindService((ServiceConnection) deviceServiceConnection);
            deviceService = null;
        }
        catch (Exception e) {
            Log.e(TAG,"Could not unbind device service. Error: "+e.getMessage());
        }
    }

和其他部分

private final ServiceConnection deviceServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName,IBinder service) {
            deviceService = ((DeviceService.LocalBinder) service).getService();
            if (!deviceService.initialize()) {
                Log.e(TAG,"Unable to initialize the device: "+deviceService.getDeviceType());
                finish();
            }
            else {
                try {
                    // Automatically connects to the device upon successful start-up initialization.
                    Log.d(TAG,"onServiceConnected. Connect to the device.");
                    deviceService.connect(deviceName);
                }
                catch (Exception e) {
                    Log.e(TAG,"Error: "+e.getMessage());
                }
            }//else
        }//onServiceConnected

        @Override
        public void onServicedisconnected(ComponentName componentName) {
            try {
                Log.d(TAG,"onServicedisconnected. disconnect from the device.");
                deviceService = null;
            }
            catch (Exception e) {
                Log.e(TAG,"Error: "+e.getMessage());
            }
        }//onServicedisconnected
    }; //deviceServiceConnection

一切正常,当我打开应用程序时,将其暂停并恢复。更改手机方向时会弹出此问题。通过调试遵循流程之后,由于重新创建了活动,因此在转到onCreate和onResume之前正确调用了onStop和onDestroy方法。日志是:

2020-09-18 11:48:17.802 31771-31771/live.fita.switchereditor D/displayActivity: Set up the toolbar.
    ...
2020-09-18 11:48:17.826 31771-31771/live.fita.switchereditor D/displayActivity: onResume. Register the device service broadcaster receiver.
    ...
2020-09-18 11:48:17.968 31771-31771/live.fita.switchereditor E/DeviceService: I'm the service: live.fita.switchereditor.UsbService@fbc1928
2020-09-18 11:48:18.060 31771-31771/live.fita.switchereditor D/displayActivity: onServiceConnected. Connect to the device.
    ...
2020-09-18 11:48:57.324 31771-31771/live.fita.switchereditor D/displayActivity: onPause. Unregister the device service broadcaster receiver.
2020-09-18 11:48:57.331 31771-31771/live.fita.switchereditor D/displayActivity: onDestroy. Unbind the device service: live.fita.switchereditor.UsbService@fbc1928
2020-09-18 11:48:57.394 31771-31771/live.fita.switchereditor D/displayActivity: Set up the toolbar.
    ...
2020-09-18 11:48:57.404 31771-31771/live.fita.switchereditor D/displayActivity: onResume. Register the device service broadcaster receiver.
2020-09-18 11:48:57.425 3831-2144/? I/Inputdispatcher: Focus entered window: Window{a4b7e56 u0 live.fita.switchereditor/live.fita.switchereditor.displayActivity}
2020-09-18 11:48:57.446 31771-31771/live.fita.switchereditor E/DeviceService: I'm the service: live.fita.switchereditor.UsbService@f432de8
2020-09-18 11:48:57.454 31771-31771/live.fita.switchereditor E/ActivityThread: Service live.fita.switchereditor.UsbService has leaked IntentReceiver live.fita.switchereditor.UsbService$1@6209dbe that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Service live.fita.switchereditor.UsbService has leaked IntentReceiver live.fita.switchereditor.UsbService$1@6209dbe that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$Receiverdispatcher.<init>(LoadedApk.java:1333)
        at android.app.LoadedApk.getReceiverdispatcher(LoadedApk.java:1114)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1412)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1385)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1373)
        at android.content.Contextwrapper.registerReceiver(Contextwrapper.java:603)
        at live.fita.switchereditor.UsbService.initialize(UsbService.java:52)
        at live.fita.switchereditor.displayActivity$2.onServiceConnected(displayActivity.java:316)
        at android.app.LoadedApk$Servicedispatcher.doConnected(LoadedApk.java:1634)
        at android.app.LoadedApk$Servicedispatcher$runconnection.run(LoadedApk.java:1663)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:251)
        at android.app.ActivityThread.main(ActivityThread.java:6589)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2020-09-18 11:48:57.472 31771-31771/live.fita.switchereditor D/displayActivity: onServiceConnected. Connect to the device.

您可以看到已注册了另一个服务,但即使通过调试也无法理解它的来源。任何想法? 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)