AIDL通信

Android Interface definition language用于多个进程之间的通信
启动其他应用里面的service在Android5.0以后不能通过隐式Intent启动,只能通过显式Intent启动。
通过setComponent(包名,类名)显式启动其他应用Service。

绑定其他应用的service并进行通信:

用AIDL通信时,需要保证AIDL文件的包名一样,然后复制AIDL文件至anotherapp。

在service中实现aidl中的接口,并在service运行时写一个输出线程。
public class AppService  extends Service {
    public AppService() {
    }



    @Override
    public IBinder  onBind(Intent intent) {
        return new IerviceRemoteBinder.Stub(){
            @Override
            public void  basicTypes( int anInt , long aLong , boolean aBoolean , float aFloat , double aDouble String aString)  throws RemoteException {

            }

            @Override
            public void  setData(String data)  throws RemoteException {
                AppService. this. data = data ;
            }
        } ;
    }

    @Override
    public void  onCreate() {
        super.onCreate() ;
        System. out.println( "service started") ;
        new Thread(){
            @Override
            public void  run() {
                super.run() ;
                running true;
                while ( running){
                    System. out.println( data) ;
                    try {
                        Thread. sleep( 1000) ;
                   catch (InterruptedException e) {
                        e.printStackTrace() ;
                    }
                }
            }
        }.start() ;
    }

    @Override
    public int  onStartCommand(Intent intent , int flags , int startId) {
        return super.onStartCommand(intent flags startId) ;

    }

    @Override
    public void  onDestroy() {
        super.onDestroy() ;
        System. out.println( "service destory") ;
        running false;
    }
    private String  data " 初始值 " ;
    private Boolean  running false;
}


在另一app中输入一个值并传入service中进行修改:
public class MainActivity  extends ActionBarActivity  implements View.OnClickListener ServiceConnection {
    EditText  input ;
    private Intent  serviceIntent ;
    @Override
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;
        setContentView(R.layout. activity_main) ;
        serviceIntent new Intent() ;
        serviceIntent.setComponent( new ComponentName( "com.example.wangxiang.remoteservicecomunication" , "com.example.wangxiang.remoteservicecomunication.AppService")) ;
        findViewById(R.id. button).setOnClickListener( this) ;
        findViewById(R.id. button2).setOnClickListener( this) ;
        findViewById(R.id. button3).setOnClickListener( this) ;
        findViewById(R.id. button4).setOnClickListener( this) ;
        findViewById(R.id. button5).setOnClickListener( this) ;
        input = (EditText) findViewById(R.id. etInput) ;
    }


    @Override
    public void  onClick(View v) {
        switch (v.getId()){
            case R.id. button:
                startService( serviceIntent) ;  //通过全路径启动另一个app里面的service
                break;
            case R.id. button2:
                stopService( serviceIntent) ;  //通过全路径停止另一个app里面的service
                break;
            case R.id. button3:
                bindService( serviceIntent ,this, Context. BIND_AUTO_CREATE) ;    
                break;
            case R.id. button4:
                unbindService( this) ;
                binder null;
                break;
            case R.id. button5:
                if( binder != null){
                    try {
                        binder.setData( input.getText().toString()) ;
                   catch (RemoteException e) {
                        e.printStackTrace() ;
                    }
                }
                break;
        }
    }

    @Override
    public void  onServiceConnected(ComponentName name IBinder service) {
        System. out.println( "bind service") ;
        System. out.println(service) ;
        binder = IerviceRemoteBinder.Stub. asInterface(service) ;
    }

    @Override
    public void  onServiceDisconnected(ComponentName name) {

    }
    private IerviceRemoteBinder  binder null;
}

AndroidManifest中的service配置:
<service
    android :name= ".AppService"
    android :enabled= "true"
    android :exported= "true" >
    <intent-filter>
        <action  android :name= "android.intent.action.AIDLService"  />
        <category  android :name= "android.intent.category.DEFAULT"  />
    </intent-filter>
</service>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值