国外的通过Binder类创建Bound Service例子Create a Bound Service



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;         }     } }
 

你可能感兴趣的:(Binder,bound)