笔记如下
首先需要注册
<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文件声明一个被调用的抽象方法
![](https://img.haomeiwen.com/i10759518/bdd60e16d7e171f6.png)
interface IService {
void callMethodInService();
}
2.一旦定义了一个aidl文件,在当前应用下就会自动创建一个.java文件
![](https://img.haomeiwen.com/i10759518/6d480efe7a86eb17.png)
3.在调用者里也创建一个与远程服务应用同包名的aidl文件,直接复制粘贴即可,可以看到在调用者的应用下也自动产生了一个.java文件
![](https://img.haomeiwen.com/i10759518/26e4252850a6d319.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的调用必须为显式调用,如果用显式调用就会以下异常
![](https://img.haomeiwen.com/i10759518/3400ddbc56c09cf7.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));
网友评论