筆記如下
首先需要注冊
<service android:name=".RemoteService">
<intent-filter>
<action android:name="com.chen.rms"/>
</intent-filter>
</service>
-
遠程通信涉及到線程間通信,需要使用aidl技術
aidl:全稱是Android Interface Definition Language,也就是Android接口定義語言
3.png
1.在遠程服務中創建一個aidl文件聲明一個被調用的抽象方法
2018-03-14_095123.png
interface IService {
void callMethodInService();
}
2.一旦定義了一個aidl文件,在當前應用下就會自動創建一個.java文件
2018-03-14_095319.png
3.在調用者里也創建一個與遠程服務應用同包名的aidl文件,直接復制粘貼即可,可以看到在調用者的應用下也自動產生了一個.java文件
2018-03-14_095635.png
在遠程調用的服務中
//class Stub extends android.os.Binder implements com.chen.remoteservice.IService
//由于Stub繼承了Binder,實現了IService接口,所以這里的代理人只需要去繼承IService類中的Stub類
private class Myagent extends IService.Stub{
@Override
public void callMethodInService() throws RemoteException {
methodInService();
}
}
與本地綁定的代理人繼承不同,因為class Stub extends android.os.Binder implements com.chen.remoteservice.IService,所以代理人直接繼承IService.Stub就可以了
在調用者中
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//綁定遠程服務
public void bindService(View v){
Intent intent = new Intent();
intent.setAction("com.chen.rms");
Intent eintent = new Intent(createExplicitFromImplicitIntent(this, intent));
bindService(eintent,new MyConntion(),BIND_AUTO_CREATE);
}
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
private IService agent;
private class MyConntion implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
agent = (IService.Stub.asInterface(service));
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
//調用遠程服務的方法
public void call(View v){
System.out.println("這是調用者調用了遠程服務.....");
try {
agent.callMethodInService();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
注意點:在android5.0及以上的版本中,servlce的調用必須為顯式調用,如果用顯式調用就會以下異常
2018-03-14_100632.png
以下是解決的辦法
/***
* Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
* "java.lang.IllegalArgumentException: Service Intent must be explicit"
*
* If you are using an implicit intent, and know only 1 target would answer this intent,
* This method will help you turn the implicit intent into the explicit form.
*
* Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
* @param context
* @param implicitIntent - The original implicit intent
* @return Explicit Intent created from the implicit original intent
*/
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
Intent eintent = new Intent(createExplicitFromImplicitIntent(this,intent));