不可以直接通过binder了。

1、先编写一个aidl文件,里边包含我们要通信的方法。(Android studio 有直接新建AIDL选项)

interface myInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString); //sayHello
String sayHello(String name); }

编译器会帮我们生成相应的java文件,不过现在在AS里面看不到。

接口中的抽象内部类Stub继承android.os.Binder类并实现myInterface接口,比较重要的方法是asInterface(IBinder)方法,该方法会将IBinder类型的对象转换成myInterface类型。

使用的时候我们只要定义好接口,再使用自动生成的Stub类即可。

2、实现Service

public class MyService extends Service{

  
  
  //实现Stub类,其实就是之前的自定义Binder
myInterface.Stub stub = new myInterface.Stub(){ @Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { }

    //重写adil文件的通信方法
@Override
public String sayHello(String name) throws RemoteException { //Toast.makeText(MyService.this,"hello"+","+name,Toast.LENGTH_SHORT).show();
       //Toast只能在UI线程
return "hello"+","+name; } }; @Nullable
@Override
public IBinder onBind(Intent intent) { Log.d("TAG","onBind");
    //返回自定义的binder
return stub;
} @Override
public void onCreate() {
super.onCreate();
Log.d("TAG","onCreate"); } @Override
public void onDestroy() {
super.onDestroy();
Log.d("TAG","onDestory");
}
}

manifest.xml

        <service android:name=".MyService"
android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.MyService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>

3、Activity

这跟之前不一样,不需要binder而是要一个接口的引用,绑定之后会将Service返回的binder转换为一个接口的实例。同样,需要自己实现ServiceConnection。

public class MainActivity extends AppCompatActivity {

    //这里用的是接口哦
private myInterface myinterface; private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { //将服务的stub(Binder)实例化我们的接口
myinterface = myInterface.Stub.asInterface(iBinder); } @Override
public void onServiceDisconnected(ComponentName componentName) { }
}; //两个按钮
Button hello,bind; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); bind = (Button)findViewById(R.id.bind);
hello = (Button)findViewById(R.id.say); bind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

          //绑定
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
bindService(intent,conn,BIND_AUTO_CREATE); }
}); hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { try {             //通信,在UI线程Toast
String temp = myinterface.sayHello("小明");
Toast.makeText(MainActivity.this,temp,Toast.LENGTH_SHORT).show(); } catch (RemoteException e) {
e.printStackTrace();
} }
}); }
}

没做特别说明的话,Service和Activity我猜测是运行在同一进程的同一线程中的,并且还是UI线程。因为可以在Service里面直接Toast。定义了远程Service后,他们就运行在不不同的进程中了,可以自己写一个sleep在service里面看看app有没有卡死来验证。还想用Toast的话,就要用消息队列了,发消息到UI线程里面来Toast

最新文章

  1. 二叉树的实现与一些基本操作(C++环境)
  2. torch 入门
  3. 【OpenStack】OpenStack系列6之Sheepdog环境搭建
  4. 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.4.Tabs控件
  5. mysql报关于用户密码1045(28000),几种处理方法 (zhuan)
  6. aggregate 和 treeAggregate 的对比
  7. svn设置
  8. php long time(1)
  9. [置顶] 如何运行用记事本写的java程序
  10. Sublime Text 高级正则查换替换功能
  11. fsck害了我很久了,必须关掉,因为他每次打卡都要推迟数十分钟。
  12. 简单vue项目脚手架(vue+webpack2.0+vuex+vue-router)
  13. 《剑指offer》数组中的逆序对
  14. Exp2 后门原理与实践 20164302 王一帆
  15. CSS3 根据屏幕大小显示内容(@media)
  16. BZOJ1968 [Ahoi2005] 约数研究
  17. Devexpress中统一设置字体样式的方法
  18. MySQL入门第三天(上)——函数与视图
  19. jmeter 填写URL链接后 不能有多余的空格。
  20. Xamarin+vs2010部署错误:error MSB6004: 指定的任务可执行文件位置\sdk\\tools\zipalign.exe”无效

热门文章

  1. ranch分析学习(一)
  2. BZOJ3894:文理分科(最大流)(同BZoj3438)
  3. sync-settings(vscode)
  4. BZOJ4602 Sdoi2016 齿轮 【带权并查集】*
  5. maven的介绍以及使用
  6. .NET/C# 使窗口永不获得焦点
  7. 优化 UWP 中图片的内存占用
  8. 接口测试框架——第四篇-url、excel内容等
  9. PHP和JS页面跳转和刷新总结
  10. springboot中 简单的SpringMvc全局异常处理