天天看點

android service的使用(三)- 與activity的互動

       本文主要講activity如何與service進行互動,這個互動很常見,如在一個音樂播放器中,service負責音樂的播放,activity負責前端UI,當你在前端對進度調節時,需要向servie發出一個指令,而service播放音樂的進度也需要及時的傳回給activity,使得activity的進度條能夠向後滑動。本文所參考的是官網。上面有詳細的例子,這裡不再詳述。 1 service和activity在同一程序時,使用IBinder        當activity需要和service互動時,采用service的第二種啟動方式,即 bindService(),将avtivity(準确的說是clients,因為其他元件也可以和service綁定)和service綁定。一方面,service需要建立自己的IBinder類,繼承Binder,override這個類的onBind()方法,在該方法中向clients傳回這個IBinder執行個體。另一方面,clients需要實作一個ServiceConnection對象(是一個内部匿名類),clients可以利用這個ServiceConnection中的onServiceConnected()方法擷取這個IBinder執行個體,在通過這個IBinder中的方法擷取service的執行個體,進而通路service的public方法。        這種方法非常适合service隻是activity的一個背景工作,再次強調,這種方法隻适于 同一程序的activity和service,在預設情況下的service和activity是在同一個程序中的,除非在manifest中另外聲明 android:process 屬性。        而且在例子中也隻有 activity向service 發送消息,service向activity發送消息還沒有找到,學渣認為可以用廣播。        這裡有一個例子,activity可以通過IBinder調用service中的方法,大家可以參考,首先是service,在service中聲明一個IBinder類的執行個體,在onBind時向clients傳回這個IBinder對象,而clients可以通過onServiceConnected()擷取IBinder執行個體,代碼如下:

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);
    }
}
           

activity的使用:

public class BindingActivity extends Activity {
    LocalService 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 LocalService
        Intent intent = new Intent(this, LocalService.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 the LocalService.
            // 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 LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}
           

2 service和activity不在同一程序   (1)使用Messager , 這是最簡單的IPC,在service中定義一個Handler用來處理不同的Message對象

  (2)使用AIDL 由于學渣現在還沒有用到這塊,暫時停一下,以後學了會繼續更新本部落格。