How can I update information in an Android Activity from a background Service

前端 未结 5 1963
北海茫月
北海茫月 2020-11-29 17:06

I am trying to create a simple Android application that has a ActivityList of information, when the application starts, I plan to start a Service that will be constantly cal

5条回答
  •  情歌与酒
    2020-11-29 17:17

    You need to use bindService() to bind the activity with running service and communicate with it.

    public class BindingActivity extends Activity {
    YourService mService;
    boolean mBound = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        // Bind to Your Service
        Intent intent = new Intent(this, YourService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }
    
    /** Called when a button is clicked (the button in the layout file attaches to
      * this method with the android:onClick attribute) */
    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from your Service.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }
    
    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {
    
        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to the running Service, cast the IBinder and get instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }
    
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
     };
    }
    

    and your service like:

    public class LocalService extends Service {
        // Binder given to clients
       private final IBinder mBinder = new LocalBinder();
       // Random number generator
       private final Random mGenerator = new Random();
    
    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    
    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
      }
    }
    

提交回复
热议问题