2018-03-13 服务Service(二)-----远程绑定

笔记如下



首先需要注册

        
            
                
            
        
  • 远程通信涉及到线程间通信,需要使用aidl技术
    aidl:全称是Android Interface Definition Language,也就是Android接口定义语言


    3.png

1.在远程服务中创建一个aidl文件声明一个被调用的抽象方法


2018-03-13 服务Service(二)-----远程绑定_第1张图片
2018-03-14_095123.png
interface IService {

   void callMethodInService();
}

2.一旦定义了一个aidl文件,在当前应用下就会自动创建一个.java文件


2018-03-13 服务Service(二)-----远程绑定_第2张图片
2018-03-14_095319.png

3.在调用者里也创建一个与远程服务应用同包名的aidl文件,直接复制粘贴即可,可以看到在调用者的应用下也自动产生了一个.java文件


2018-03-13 服务Service(二)-----远程绑定_第3张图片
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  =  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 = 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));  

你可能感兴趣的:(2018-03-13 服务Service(二)-----远程绑定)