Activity呼叫Service的方法
阿新 • • 發佈:2019-01-28
一般來說,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) {
}
};
}