国外的通过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;

        }

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值