Harshit Syal
Harshit Syal

Reputation: 659

Unexpected error when changing to landscape mode android

I am experiencing some unexpected service leaked error when I change activity to landscape mode. The app is running fine, the error only shows up in logcat.

logcat error is :

E/ActivityThread(16640): Activity com.amazon.hsyal.ui.VoltageSODLoggerActivity has leaked ServiceConnection com.amazon.hsyal.ui.VoltageSODLoggerActivity$1@41461908 that was originally bound here

E/ActivityThread(16640): android.app.ServiceConnectionLeaked: Activity com.amazon.hsyal.ui.VoltageSODLoggerActivity has leaked ServiceConnection com.amazon.hsyal.ui.VoltageSODLoggerActivity$1@41461908 that was originally bound here

E/ActivityThread(16640): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:999)

E/ActivityThread(16640): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:893)

E/ActivityThread(16640): at android.app.ContextImpl.bindService(ContextImpl.java:1139)

E/ActivityThread(16640): at android.content.ContextWrapper.bindService(ContextWrapper.java:386)

E/ActivityThread(16640): at com.amazon.hsyal.ui.VoltageSODLoggerActivity$2.onClick(VoltageSODLoggerActivity.java:96)

E/ActivityThread(16640): at android.view.View.performClick(View.java:3538)

E/ActivityThread(16640): at android.view.View$PerformClick.run(View.java:14132)

E/ActivityThread(16640): at android.os.Handler.handleCallback(Handler.java:605)

E/ActivityThread(16640): at android.os.Handler.dispatchMessage(Handler.java:92)

E/ActivityThread(16640): at android.os.Looper.loop(Looper.java:137)

E/ActivityThread(16640): at android.app.ActivityThread.main(ActivityThread.java:4492)

E/ActivityThread(16640): at java.lang.reflect.Method.invokeNative(Native Method)

E/ActivityThread(16640): at java.lang.reflect.Method.invoke(Method.java:511)

E/ActivityThread(16640): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

E/ActivityThread(16640): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

E/ActivityThread(16640): at dalvik.system.NativeStart.main(Native Method)

corresponding onCreate code is:

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");

    }

}

I think there must be some problem in where I am creating instace of service connection. Presently I am doing that in onCreate, do I need to do this some where else also ?

Upvotes: 0

Views: 529

Answers (2)

Zoltán
Zoltán

Reputation: 22206

When the orientation changes, the activity is recreated, i.e. onDestroy() is invoked and onCreate is invoked again.

You may have forgotten to release your wake lock in the onDestroy() method. Make your wake lock a member variable and invoke

wk.release();

in your onDestroy() method.

Upvotes: 1

herom
herom

Reputation: 2542

in your code, I can't see an instance of your service connection, only an Intent (which is not a connection to your service, as you might know)...

you probably want to bind your service in the onStart() and unbind your service in the onStop() methods of your activity. also, I would suggest that you'd move your WakeLock in your Service class, if you don't need your Activity to show up all the way...

see http://developer.android.com/reference/android/app/Service.html for more details about the Service class and how to bind/unbind from a Service (instead of using startService() and stopService())

Upvotes: 1

Related Questions