0

これは非常にばかげたものであり、コードがログキャットにエラーを出力するのが好きではないという理由だけで、私はそれについて尋ねています。

というサービスがありますBackgroundCollectorProcess。まだ報告されていないデータベース内の行を処理するのが主な仕事です。これは、XMPP 接続を介して行われます。だから、onStartCommand私は次のものを持っています:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.d("BGCP", "Spinning");

        // Bind to our XmppConnector service to post any waiting transactions
        Intent iXmpp = new Intent(getApplicationContext(), BackgroundXmppConnector.class);
        bindService(iXmpp, mConnection, Context.BIND_AUTO_CREATE);      

        // Start the sending of all collected logs
        new DeliveriesSenderTask().execute();
        new GeoLogSenderTask().execute();

        /*
            WARNING!!

            You can't call unbindService(mConnection) here - as the two main threads
            for posting delivery and geolocation transactions wont have completed in
            time. Thus, leaving this object unable to complete it's task.

            Logcat will throw up an "error" - saying about a resource binding being
            leaked in this class. 

            TODO: Revisit this once there is a definitive answer to the problem.
         */

        // Commit suicide. BackgroundCollectorKicker will ressurect me. 
        stopSelf();

        return Service.START_NOT_STICKY;
    }

コメント用の大量のテキスト ウォールからわかるように、愚かな logcat エラーが発生しました。それは次のとおりです。

05-16 15:30:26.243: E/ActivityThread(16078): Service com.goosesys.gaggle.services.BackgroundCollectorProcess has leaked ServiceConnection com.goosesys.gaggle.services.BackgroundCollectorProcess$1@449ddff0 that was originally bound here
05-16 15:30:26.243: E/ActivityThread(16078): android.app.ServiceConnectionLeaked: Service com.goosesys.gaggle.services.BackgroundCollectorProcess has leaked ServiceConnection com.goosesys.gaggle.services.BackgroundCollectorProcess$1@449ddff0 that was originally bound here
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.ContextImpl.bindService(ContextImpl.java:1507)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.ContextImpl.bindService(ContextImpl.java:1496)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-16 15:30:26.243: E/ActivityThread(16078):    at com.goosesys.gaggle.services.BackgroundCollectorProcess.onStartCommand(BackgroundCollectorProcess.java:70)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2681)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.ActivityThread.access$1900(ActivityThread.java:146)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.os.Looper.loop(Looper.java:137)
05-16 15:30:26.243: E/ActivityThread(16078):    at android.app.ActivityThread.main(ActivityThread.java:5171)
05-16 15:30:26.243: E/ActivityThread(16078):    at java.lang.reflect.Method.invokeNative(Native Method)
05-16 15:30:26.243: E/ActivityThread(16078):    at java.lang.reflect.Method.invoke(Method.java:511)
05-16 15:30:26.243: E/ActivityThread(16078):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
05-16 15:30:26.243: E/ActivityThread(16078):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
05-16 15:30:26.243: E/ActivityThread(16078):    at dalvik.system.NativeStart.main(Native Method)

これらは AsyncTasks です:

    private class DeliveriesSenderTask extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected Void doInBackground(Void... arg0) 
        {
            try
            {
                doPostDeliveries();
            }
            catch(Exception ex)
            {
                Utility.writeExceptionToLog(getApplicationContext(), ex);
            }           
            return null;
        }       
    }

    private class GeoLogSenderTask extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected Void doInBackground(Void... params) 
        {       
            try
            {
                doPostGeoLogs();
            }
            catch(Exception ex)
            {
                Utility.writeExceptionToLog(getApplicationContext(), ex);
            }
            return null;
        }       
    }

ここで、実行するタスク自体が非常に時間がかかる (またはかかる可能性がある) ことを考えると、それらは別のスレッドで実行する必要があります。unbindService()したがって、スレッドが終了していないため、を呼び出すことはできません。

私の質問は...いつ電話するのが最適unbindServiceですか? 明らかに、完了したらそれを呼び出そうとしましたGeoLogSenderTaskが、メイン スレッドが 2 つの asynctasks の前に終了するため、LogCat がこのばかげたメッセージを出力します。

どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1