quran / quran_android

a quran reading application for android
http://android.quran.com
GNU General Public License v3.0
1.99k stars 882 forks source link

audio service not stopped when app quits and not playing audio #55

Closed ahmedre closed 12 years ago

ahmedre commented 13 years ago

per report from email.

mahmoudhossam commented 13 years ago

I'm also affected by this issue, the audio service starts even no audio was played.

here's a screenshot

service

afarra commented 12 years ago

I noticed quite a bit of reports and annoyment with this issue on Market comments recently.

I also noticed that as soon as I start the app or do anything, I get a lot of "leaked service" errors:

ERROR/ActivityThread(2759): Activity com.quran.labs.androidquran.QuranViewActivity has leaked ServiceConnection com.quran.labs.androidquran.QuranViewActivity$1@405bef30 that was originally bound here android.app.ServiceConnectionLeaked: Activity com.quran.labs.androidquran.QuranViewActivity has leaked >ServiceConnection com.quran.labs.androidquran.QuranViewActivity$1@405bef30 that was originally bound here at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:938) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:833) at android.app.ContextImpl.bindService(ContextImpl.java:971) at android.content.ContextWrapper.bindService(ContextWrapper.java:347) at com.quran.labs.androidquran.QuranViewActivity.bindAudioService(QuranViewActivity.java:540) at com.quran.labs.androidquran.QuranViewActivity.onCreate(QuranViewActivity.java:90) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836) at android.app.ActivityThread.access$1600(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method)

I noticed in the code startService is used. Is that correct?

    private void bindAudioService() {
        if (!bounded) {
            Intent serviceIntent = new Intent(getApplicationContext(),
                    QuranAudioService.class);
            startService(serviceIntent);
            bounded = bindService(serviceIntent, conn, BIND_AUTO_CREATE);
            Log.d("QuranView", "Audio service bounded: " + bounded);
        }
    }

According to the JavaDocs:

Using startService() overrides the default service lifetime that is managed by bindService: it requires the service to remain running until stopService is called, regardless of whether any clients are connected to it.

Whereas for bindService:

The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.

Do we want it to live even when context from which it is called is destroyed?

See also: Similar issue on StackOverflow

ahmedre commented 12 years ago

although i haven't yet looked at this code in detail, i think it's modeled off of what we did in the download service. in the download service, we start if it's not started and then bind (rather than just bind), the reason being that we don't want the download to quit if the activity dies. i imagine audio is the same case.

ahmedre commented 12 years ago

in theory, a call to unbindService onDestroy should do the trick, however, i tried this and i get an exception about the service not being bound (despite using the same context and the same connection). not sure if the connection variable is gone at this point or what exactly is going on here... have to look at it in more detail.

hmaher commented 12 years ago

fixed by @ahmedre