How to have Android Service communicate with Activity

前端 未结 13 1885
说谎
说谎 2020-11-22 02:54

I\'m writing my first Android application and trying to get my head around communication between services and activities. I have a Service that will run in the background an

13条回答
  •  温柔的废话
    2020-11-22 03:18

    Binding is another way to communicate

    Create a callback

    public interface MyCallBack{
    
       public void getResult(String result);
    
    }
    

    Activity side:

    1. Implement the interface in the Activity

    2. Provide the implementation for the method

    3. Bind the Activity to Service

    4. Register and Unregister Callback when the Service gets bound and unbound with Activity.

      public class YourActivity extends AppCompatActivity implements MyCallBack{
      
            private Intent notifyMeIntent;
            private GPSService gpsService;
            private boolean bound = false;
      
            @Override
            public void onCreate(Bundle sis){
      
                // activity code ...
      
                startGPSService();
      
            }
      
            @Override
            public void getResult(String result){
             // show in textView textView.setText(result);
            }
      
            @Override
            protected void onStart()
            {
                super.onStart();
                bindService();
            }
      
            @Override
            protected void onStop() {
                super.onStop();
                unbindService();
            }
      
            private ServiceConnection serviceConnection = new ServiceConnection() {
      
                  @Override
                  public void onServiceConnected(ComponentName className, IBinder service) {
      
                        GPSService.GPSBinder binder = (GPSService.GPSBinder) service;
                        gpsService= binder.getService();
                        bound = true;
                        gpsService.registerCallBack(YourActivity.this); // register
      
                 }
      
                 @Override
                 public void onServiceDisconnected(ComponentName arg0) {
                        bound = false;
                 }
            };
      
            private void bindService() {
      
                 bindService(notifyMeIntent, serviceConnection, Context.BIND_AUTO_CREATE);
            }
      
            private void unbindService(){
                 if (bound) {
                       gpsService.registerCallBack(null); // unregister            
                       unbindService(serviceConnection);
                       bound = false;
                  }
            }
      
            // Call this method somewhere to start Your GPSService
            private void startGPSService(){
                 notifyMeIntent = new Intent(this, GPSService.class);
                 startService(myIntent );
            }
      
       }
      

    Service Side:

    1. Initialize callback

    2. Invoke the callback method whenever needed

       public class GPSService extends Service{
      
           private MyCallBack myCallback;
           private IBinder serviceBinder = new GPSBinder();
      
           public void registerCallBack(MyCallBack myCallback){
                this.myCallback= myCallback;
           }
      
           public class GPSBinder extends Binder{
      
               public GPSService getService(){
                    return GPSService.this;
               }
          }
      
          @Nullable
          @Override
          public IBinder onBind(Intent intent){
               return serviceBinder;
          }
       }
      

提交回复
热议问题