Android使用aidl来绑定远程服务
2016-02-18 16:15
411 查看
绑定远程服务流程
1、在activity中绑定调用bindService去绑定服务
bindService(intent, conn, flags)
2、在服务里面 需要重写方法 onBind 在服务被绑定的时候需要返回一个代理人,调用返回一个IBinder,这个代理人必须要实现一个方法,这个方法能否调用服务中的方法
3、在Activity中onServiceConnected中得到代理人
4、调用代理人中的方法。
案例:
一个应用:
package com.example.RemoteService;
import com.example.AlipayService.IService;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private Intent intent;
private IService iservice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
intent.setAction("com.eastmoney.Alipay");
}
public void bind(View view){
bindService(intent, new Myconn(), BIND_AUTO_CREATE);
}
private class Myconn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//强制內型转换为IService的接口类型
iservice = IService.Stub.asInterface(service) ;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void call(View view){
try {
iservice.callmethodInService();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.example.AlipayService;
interface IService {
void callmethodInService();
}
package com.example.AlipayService;
interface IService {
void callmethodInService();
}
1、在activity中绑定调用bindService去绑定服务
bindService(intent, conn, flags)
2、在服务里面 需要重写方法 onBind 在服务被绑定的时候需要返回一个代理人,调用返回一个IBinder,这个代理人必须要实现一个方法,这个方法能否调用服务中的方法
3、在Activity中onServiceConnected中得到代理人
4、调用代理人中的方法。
案例:
一个应用:
package com.example.RemoteService;
import com.example.AlipayService.IService;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private Intent intent;
private IService iservice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
intent.setAction("com.eastmoney.Alipay");
}
public void bind(View view){
bindService(intent, new Myconn(), BIND_AUTO_CREATE);
}
private class Myconn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//强制內型转换为IService的接口类型
iservice = IService.Stub.asInterface(service) ;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void call(View view){
try {
iservice.callmethodInService();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.example.AlipayService;
interface IService {
void callmethodInService();
}
package com.example.AlipayService;
interface IService {
void callmethodInService();
}
相关文章推荐
- Android 进程间通信之AIDL
- android重复的文件复制APK META-INF许可证错误记录
- Android Studio添加assets文件夹
- Android 拍照获取图片并剪切
- 最全的android开发资料大集合
- 关于android postDelayed
- 阅读《Android 从入门到精通》(26)——上下文菜单
- Android 实现预览,录像,拍照
- android 判断横竖屏的方法
- 清单文件 installLocation简析
- Android相机开发那些坑
- Android反编译工具的使用-Android Killer
- 第二章 画图
- android简单实例-----------5种Notification的简单使用
- [android]_[内存泄露分析和排查]
- Android中Canvas绘图基础详解(附源码下载)
- Android设备重启(reboot)---Android studio
- android设置横竖屏切换时生命周期不会改变
- Android多点触控技术,实现对图片的放大缩小平移,惯性滑动等功能
- 第五章 安卓命名