当我将活动更改为横向模式时,我遇到了一些意外的服务泄漏错误。该应用程序运行良好,错误仅显示在 logcat 中。
logcat 错误是:
E/ActivityThread(16640): Activity com.amazon.hsyal.ui.VoltageSODLoggerActivity 泄露了ServiceConnection com.amazon.hsyal.ui.VoltageSODLoggerActivity$1@41461908 原来绑定在这里
E/ActivityThread(16640): android.app.ServiceConnectionLeaked: Activity com.amazon.hsyal.ui.VoltageSODLoggerActivity 已经泄露了原来绑定在这里的ServiceConnection com.amazon.hsyal.ui.VoltageSODLoggerActivity$1@41461908
E/ActivityThread(16640):在 android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:999)
E/ActivityThread(16640):在 android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:893)
E/ActivityThread(16640):在 android.app.ContextImpl.bindService(ContextImpl.java:1139)
E/ActivityThread(16640):在 android.content.ContextWrapper.bindService(ContextWrapper.java:386)
E/ActivityThread(16640):在 com.amazon.hsyal.ui.VoltageSODLoggerActivity$2.onClick(VoltageSODLoggerActivity.java:96)
E/ActivityThread(16640):在 android.view.View.performClick(View.java:3538)
E/ActivityThread(16640):在 android.view.View$PerformClick.run(View.java:14132)
E/ActivityThread(16640):在 android.os.Handler.handleCallback(Handler.java:605)
E/ActivityThread(16640):在 android.os.Handler.dispatchMessage(Handler.java:92)
E/ActivityThread(16640):在 android.os.Looper.loop(Looper.java:137)
E/ActivityThread(16640):在 android.app.ActivityThread.main(ActivityThread.java:4492)
E/ActivityThread(16640):在 java.lang.reflect.Method.invokeNative(Native Method)
E/ActivityThread(16640):在 java.lang.reflect.Method.invoke(Method.java:511)
E/ActivityThread(16640):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/ActivityThread(16640):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/ActivityThread(16640):在 dalvik.system.NativeStart.main(Native Method)
对应的onCreate代码为:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager mgr= (PowerManager) this.getSystemService(Context.POWER_SERVICE);
wk = mgr.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyWakeLock");
wk.acquire();
intent = new Intent(getBaseContext(), LoggerService.class);
Button start_stop_button = (Button) findViewById(R.id.start_stop_button);
start_stop_button.setOnClickListener(startStopButtonListener);
if(getServiceInfo(this)){
start_stop_button.setText("Stop Service");
}else
{ start_stop_button.setText("Start Service");
}
}
我认为在创建服务连接实例的地方一定存在一些问题。目前我正在 onCreate 中执行此操作,我还需要在其他地方执行此操作吗?