2

私は現在、歩数計アプリケーションで作業中です。最初は、私は1つの活動、歩数計活動から始めました。このアクティビティは、バックグラウンドで実行されるはずのサービスを開始し、そのサービスにバインドします。コードが長いので、私は私の質問に役立つと思うもののセクションを与えるだけです。サービスタブ間でローカルサービスを実行する方法は?

/*Local service binding*/ 
    public class PedometerBinder extends Binder { 
     public PedometerService getService() { 
      return PedometerService.this; 
     } 
    } 


    /*A client is binding to the service with bindService() 
    * Returns the IBinder object received in 
    * ServiceConnection.onServiceConnected(ComponentName,IBinder)*/ 
    @Override 
    public IBinder onBind(Intent intent) { 
     return new PedometerBinder(); 
    } 

を拡張するサービスクラスで

//Bind service 
    private ServiceConnection mServiceConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, IBinder service) { 

      //binder to communicate with the service 
      PedometerService.PedometerBinder mBinder = (PedometerService.PedometerBinder)service; 

      mPedometerService = mBinder.getService(); 
      mPedometerService.registerCallback(mCallback); 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      mPedometerService = null; 
     } 
    }; 

private void startPedometerService() { 
     if (!isPedometerService) { 
      Log.v(TAG, "Start service"); 
      isPedometerService = true; 

      //start service 
      Intent intent = new Intent(getApplicationContext(), 
        PedometerService.class); 
      startService(intent); 
     } 
    } 

    //Bind to the service 
    private void bindPedometerService() { 
     Log.i(TAG, "Bind service"); 

     Intent intent = new Intent(PedometerActivity.this, PedometerService.class); 
     bindService(intent, mServiceConnection, 
       Context.BIND_AUTO_CREATE + Context.BIND_DEBUG_UNBIND); 
    } 

    //close connection with service 
    private void unbindPedometerService() { 
     Log.i(TAG, "Unbind service"); 
     unbindService(mServiceConnection); 
    } 

    //Stop the service that had been started 
    private void stopPedometerService() { 
     Log.i(TAG, "Stop service"); 
     if (mPedometerService != null) { 

      //stop service 
      Intent intent = new Intent(PedometerActivity.this, PedometerService.class); 
      stopService(intent); 

      isPedometerService = false; 
     } 
    } 

@Override 
    public void onResume() { 
     Log.i(TAG, "onResume"); 
     super.onResume(); 

     startPedometerService(); 
     bindPedometerService(); 


    } 


    @Override 
    public void onStop() { 
     super.onStop(); 
     Log.i(TAG, "onStop"); 
     stopPedometerService(); 

    } 

    public void onDestroy() { 
     super.onDestroy(); 

     unbindPedometerService(); 
     stopPedometerService();   

    } 

私はその後、私のアプリケーションは、それ故に3つのタブ3つの断片とtablayoutているように変更。私はPedometerFragmentにPedometerActivityからコードを貼り付けると、問題のトラブル私はタブを切り替えたときに実行されているサービスを維持するが生じていますされ

//Bind service 
    private ServiceConnection mServiceConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, IBinder service) { 

      //binder to communicate with the service 
      PedometerService.PedometerBinder mBinder = (PedometerService.PedometerBinder)service; 

      mPedometerService = mBinder.getService(); 
      mPedometerService.registerCallback(mCallback); 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      mPedometerService = null; 
     } 
    }; 

private void startPedometerService() { 
     if (!isPedometerService) { 
      Log.v(TAG, "Start service"); 
      isPedometerService = true; 

      //start service 
      Intent intent = new Intent(getActivity().getApplicationContext(), 
        PedometerService.class); 
      getActivity().startService(intent); 
     } 
    } 

    //Bind to the service 
    private void bindPedometerService() { 
     Log.i(TAG, "Bind service"); 

     Intent intent = new Intent(getActivity(), PedometerService.class); 
     getActivity().bindService(intent, mServiceConnection, 
       Context.BIND_AUTO_CREATE + Context.BIND_DEBUG_UNBIND); 
    } 

    //close connection with service 
    private void unbindPedometerService() { 
     Log.i(TAG, "Unbind service"); 
     getActivity().unbindService(mServiceConnection); 
    } 

    //Stop the service that had been started 
    private void stopPedometerService() { 
     Log.i(TAG, "Stop service"); 
     if (mPedometerService != null) { 

      //stop service 
      Intent intent = new Intent(getActivity(), PedometerService.class); 
      getActivity().stopService(intent); 

      isPedometerService = false; 
     } 
    } 

@Override 
    public void onResume() { 
     Log.i(TAG, "onResume"); 
     super.onResume(); 

     startPedometerService(); 
     bindPedometerService(); 


    } 


    @Override 
    public void onStop() { 
     super.onStop(); 
     Log.i(TAG, "onStop"); 
     stopPedometerService(); 

    } 

    public void onDestroy() { 
     super.onDestroy(); 

     unbindPedometerService(); 
     stopPedometerService();   

    } 

それを修正しました。 FragmentStatePagerAdapterを使用しているため、最後のタブに移動すると、最初のフラグメント(PedometerFragment)がアンロードされます。 onSaveInstanceStateに他の変数を保存することができましたが、すべてがすべて再開されるため、これは役に立たないようです。

答えて

1

サービスをActivityクラスにバインドする必要があります。次に、接続された任意のFragmentから、インタフェースまたはパブリックメソッドを使用して使用できます。

+0

うまくいきました。フラグメントライフサイクルメソッドがサービスを中断しませんでした – facilitator

+0

フラグメントライフサイクルメソッドDIDがサービスを中断しました(自分で言った) – Mars

+0

フラグメントライフサイクルメソッドのオーバーライド* – Mars

1

あなたはstartServiceを使用しています。したがって、バインドされたコンポーネントが破棄されても、サービスを停止しないでください。ただし、onStop()に明示的にstopPedometerService()を呼び出しています。これは、フラグメントが開始されなくなったときに呼び出されます。

stopPedometerService()onStop()onDestroy()から簡単に削除してみてください。

参照:多数のページ は、より多くのリストビューのように働いて、あるときhttps://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

ページャのこのバージョンは、より有用です。ページがユーザーの に表示されない場合、そのフラグメント全体が破棄され、そのフラグメントの保存状態が維持されます( )。

+0

私はonStopでstopServiceメソッド呼び出しを削除しましたが、サービスは中断されませんでしたが、より多くの問題が発生しました。 @Stanislav Bondarソリューションが最高です。 FragmentStatePagerAdapterの実装は、問題とは独立していました。ありがとう – facilitator

+0

Stanislavのソリューションは実際には解決策ではありません...あなたのサービスが停止していた理由を知りたかったので、私はあなたにそれを示しました。あなたは活動にもバインドする必要はありません。 – Mars

+0

私はあなたのonDestroy()でstopPedometerServiceを持っていたことを忘れていましたので、私の回答を更新しました – Mars

関連する問題