service跨进程远程通讯

AIDL(Android Interface Definition Language)是Android接口定义语言的意思,它可以用于让某个Service与多个应用程序组件之间进行跨进程通信,从而可以实现多个应用程序共享同一个Service的功能
服务端:
首先需要新建一个AIDL文件,在这个文件中定义好Activity需要与Service进行通信的方法。新建MyAIDLService.aidl文件,代码如下所示:
包名必须跟所在包名一致

package service.myaidl;
interface MyAidlTest {  
    int plus(int a, int b);  
    String toUpperCase(String str);  
}  

点击保存之后,gen目录下就会生成一个对应的Java文件
然后在MyService里实现它的方法,然后在onBind把mStub return出去
远程启动service需要隐式启动 所以需要在mianifest里添加动作

 <service android:name="com.example.servicepractice.MyService">
            <intent-filter >
                <action android:name="service.myaidl.MyAidlTest"/>
            </intent-filter>
        </service>
package com.example.servicepractice;

import service.myaidl.MyAidlTest;
import service.myaidl.MyAidlTest.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.speech.tts.Voice;

public class MyService extends Service {

    private MyBind myBind = new MyBind();
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return mStub;
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        UserLog.Print("oncreate");
        super.onCreate();
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        UserLog.Print("onDestroy");
        super.onDestroy();
    }
    /* (non-Javadoc)
     * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
     * startId service启动的次数
     */
    @Override   
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        UserLog.Print("onStartCommand"+flags+"---"+startId);
        return super.onStartCommand(intent, flags, startId);
    }
    class MyBind extends Binder{
        public void downlod(){
            System.out.println("执行下载任务");
        }
    }
      MyAidlTest.Stub mStub = new Stub() {

        @Override
        public String toUpperCase(String str) throws RemoteException {
            // TODO Auto-generated method stub
              if (str != null) {  
                  return str.toUpperCase();  
              }  
              return null; 
        }

        @Override
        public int plus(int a, int b) throws RemoteException {
            // TODO Auto-generated method stub
              return a + b;  
        }
    };
}

然后在MainActivity 修改serviceConnection
创建MyAidlTest 对象在onServiceConnected接收service
使用了MyAidlTest .Stub.asInterface()方法将传入的IBinder对象传换成了MyAidlTest 对象
然后重运行下服务端,这样服务端就完成了。

public class MainActivity extends Activity {
    private Intent itIntent ;
    private MyService.MyBind mBind;
    private EditText etEditText;
    private MyAidlTest myAidlTest;
    private ServiceConnection serviceConnection =new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            System.out.println("-------");
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            int a=0;
            myAidlTest = MyAidlTest.Stub.asInterface(service);
            try {
                a=myAidlTest.plus(2, 7);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("-----"+a);
        }
    };

客户端:
复制服务端的aidl包 添加到客户端里
然后通过隐式意图启动service 具体代码如下:

package com.example.client;

import service.myaidl.MyAidlTest;
import service.myaidl.MyAidlTest.Stub;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {
    private MyAidlTest myAidlTest;
    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            int a =0;
            myAidlTest = MyAidlTest.Stub.asInterface(service);
            try {
                a = myAidlTest.plus(5, 3);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----"+a);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void start(View view){
        Intent itIntent = new Intent("service.myaidl.MyAidlTest");
        bindService(itIntent,serviceConnection, BIND_ABOVE_CLIENT);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值