Android编程学习到Binder通信,网络上查了不少资料。看了《Android深入浅出之Binder机制》http://www.cnblogs.com/innost/archive/2011/01/09/1931456.html

还是似懂非懂,在网络上搜索视频,国内没有找到,就到外面去溜达一下,发现一个简单的例子。可惜它仅实现一个app内部的通信。回头在去看Android深入浅出之Binder机制。既然binderandroid的重要通信手段,我也把看到的内容分享给各位。希望对大伙有帮助

演示如何通过扩展Binder类创建一个Android Bound Service

创建一个工程Create a Bound Service,一个空的Activity

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="获取第一个消息From service"  android:onClick="getFirstServiceMessage"  android:id="@+id/button"  android:layout_alignParentTop="true"  android:layout_centerHorizontal="true" />  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="获取第一个消息From service"  android:id="@+id/button2"  android:onClick="getSecondServiceMessage"  android:layout_below="@+id/button"  android:layout_centerHorizontal="true"  android:layout_marginTop="88dp" />  <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:textAppearance="?android:attr/textAppearanceLarge"  android:text="没有消息From service"  android:id="@+id/textView"  android:layout_below="@+id/button2"  android:layout_centerHorizontal="true"  android:layout_marginTop="77dp" /> </RelativeLayout>

新建一个类MyService extends Service;并为其实现onBind接口

编写两个消息方法getFirstMessagegetSecondMessage

package com.czg.com.learncreateboundservice; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; /** * Created by Administrator on 2016/1/6. */ public class MyService extends Service {  private final IBinder mBinder=new LocalService();  @Nullable  @Override  public IBinder onBind(Intent intent) {  return mBinder;  }  public class LocalService extends Binder{  MyService getService(){  return MyService.this;  }  }  public String getFirstMessage(){  return "大家好";  }  public String getSecondMessage(){  return "这是 bound Service 例子";  } }

AndroidManifest.xml中注册MyService

<service android:name=".MyService"></service>

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.czg.com.learncreateboundservice" >  <application  android:allowBackup="true"  android:icon="@mipmap/ic_launcher"  android:label="@string/app_name"  android:supportsRtl="true"  android:theme="@style/AppTheme" >  <activity android:name=".MainActivity" >  <intent-filter>  <action android:name="android.intent.action.MAIN" />  <category android:name="android.intent.category.LAUNCHER" />  </intent-filter>  </activity>  <service android:name=".MyService"></service>  </application> </manifest>

MainActivity.java
package com.czg.com.learncreateboundservice; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity {  TextView textView;  MyService myService; //连接的服务器实例   Boolean isBind=false; //是否连接MyService服务器   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  textView= (TextView) findViewById(R.id.textView);  Intent intent=new Intent(this,MyService.class);  bindService(intent,MConnection, Context.BIND_AUTO_CREATE);  }  public void getFirstServiceMessage(View view) {  String message;  message=myService.getFirstMessage();  textView.setText(message);  }  public void getSecondServiceMessage(View view) {  String message;  message=myService.getSecondMessage();  textView.setText(message);  }  //定义服务连接接口变量,并实现onServiceConnectedonServiceDisconnected  private ServiceConnection MConnection=new ServiceConnection() {  @Override  public void onServiceConnected(ComponentName name, IBinder service) {  MyService.LocalService localService= (MyService.LocalService) service;  myService=localService.getService();  isBind=true;  }  @Override  public void onServiceDisconnected(ComponentName name) {  isBind=false;  }  };  //程序退出时断开服务器连接   @Override  protected void onStop() {  super.onStop();  if (isBind){  unbindService(MConnection);  isBind=false;  }  } }
 

更多相关文章

  1. Android中Thread,hanlder(HanlderThread),Runnable之间的关系
  2. android 消息机制 Handler Looper 原理分析
  3. Adroid 使用AIDL和远程服务实现进程通信
  4. Android中跨进程通信的IPC机制(Binder框架)
  5. android第三方应用跳转到QQ并指定QQ号聊天
  6. Android(安卓)使用Messenger实现跨app通信
  7. 数字签名的案例说明
  8. [置顶] Android中AIDL实现进程通信(附源码下载)
  9. Android(安卓)App开发基础篇—实现非阻塞Socket通信

随机推荐

  1. 如何获得MySQL中某一行的偏移量?
  2. mysql-5.7.10-winx64 绿色版安装办法
  3. mysql主从复制配置操作以及主主配置宕机
  4. 如何使用PDO从MySQL获取正确的数据类型?
  5. MySQL基本操作汇总
  6. linux运维必会MySQL企业面试题
  7. mysql 中使用聚合函数sum()后出现很长的
  8. mysql学习--1.事务
  9. 获取喜欢和评论的帖子 - 一个查询?
  10. MySQL修改表结构操作命令总结