android 远程调用服务,android调用远程service(一个apk调用另一个apk的service)

根据android的binder,以及网上aidl的例子,编写了一个调用远程service的例子.

server//

1.aidl

建立com.easymorse包,aidl文件为ICountService.aidl

文件内容:

package com.easymorse;

interface ICountService{

int getCount();

}

2.service

public class CountService extends Service {

private boolean threadDisable;

private int count;

public ICountService.Stub serviceBinder=new ICountService.Stub(){

@Override

public int getCount() throws RemoteException {

return count;

}

};

@Override

public IBinder onBind(Intent intent) {

count = 103;

Log.v("zxl---onBind", "Count is " + count);

return serviceBinder;

}

@Override

public void onCreate() {

super.onCreate();

}

@Override

public void onDestroy() {

super.onDestroy();

this.threadDisable = true;

Log.v("zxl---CountService", "on destroy");

}

}

3.AndroidManifest.xml

android:name="com.example.myservice.CountService">

server//

client//

1.aidl

建立com.easymorse包,aidl文件为ICountService.aidl

文件内容:

package com.easymorse;

interface ICountService{

int getCount();

}

2.调用

public class MyClientActivity extends Activity {

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

System.out.println("zxl---onServiceDisconnected---"+name);

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

ICountService iCountService = ICountService.Stub.asInterface(service);

try {

System.out.println("zxl---onServiceConnected---"+iCountService.getCount());

} catch (RemoteException e) {

e.printStackTrace();

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my_client);

Intent service = new Intent("com.easymorse.CountService");

bindService(service, serviceConnection, BIND_AUTO_CREATE);

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

unbindService(serviceConnection);

super.onDestroy();

}

}

client//

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值