一般來說,Activity調用Service 分為兩種:進程內調用和進程間調用。進程內調用時比較常用的一種,在進程內調用中我們常常使用的是bindService來啟動Service(關於這種啟動方式的好處,才疏學淺就不再這賣弄了)。下面就這兩種調用方式分別進行簡單介紹:
1.通過bindService來啟動Service,並調用Service中的方法。
1.一個簡單的Service:
package gu.chuan.hang; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new MyBinder(); } public class MyBinder extends Binder{ public MyService getMyService(){ return MyService.this; } } /** * 在Service中定義這個方法,用於測試 * @return */ public String getAuthorName(){ return "guchuanhang"; } }
2.在Activity中綁定Service並測試效果:
package gu.chuan.hang; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent myServiceIntent = new Intent(MainActivity.this, MyService.class); bindService(myServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); } ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { MyService myService = ((MyService.MyBinder) service).getMyService(); String authorName = myService.getAuthorName(); Toast.makeText(MainActivity.this, "author name is: " + authorName, Toast.LENGTH_LONG).show(); } @Override public void onServiceDisconnected(ComponentName name) { } }; }
3.截個圖吧,更清楚一點: 
2.進程間啟動Service並調用Service中的方法
1.首先定義一個aidl文件(擴展名為.aidl):
package gu.chuan.hang.remoteservice.aidl; interface AuthorInfo{ String getAuthorName(); }
2.定義一個實現了aidl的服務:
package gu.chuan.hang.remoteservice.aidl; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MyService extends Service{ @Override public IBinder onBind(Intent intent) { return new MyStub(); } private class MyStub extends AuthorInfo.Stub{ @Override public String getAuthorName() throws RemoteException { return "guchuanhang"; } } }
3.在同一個app中模擬進程間調用的效果,給Service設置remote屬性:
<service android:name="gu.chuan.hang.remoteservice.aidl.MyService" android:process=":remote" android:exported="false" > </service>
4.啟動並調用Service中的方法:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Intent intent=new Intent(this,MyService.class); startService(intent); bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); } private ServiceConnection mServiceConnection=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { AuthorInfo authorInfo=AuthorInfo.Stub.asInterface(service); try { String authorName=authorInfo.getAuthorName(); Toast.makeText(MainActivity.this, "author name is: "+authorName, Toast.LENGTH_LONG).show(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } }; }
5.附上效果截圖:
6.關於aidl進程間調用是參考了http://www.cnblogs.com/zhangdongzi/archive/2012/01/09/2317197.html
大神之作,進行的精簡修改。
