Android Service(绑定BindService)

来源:互联网 发布:ubuntu skype安装包 编辑:程序博客网 时间:2024/05/16 05:41

这里写图片描述

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

BindService.java

package shortcut.song.com.myapplication;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 2017/8/14 0014. */public class BindService extends Service {    private int count;    private boolean quit;    // 定义onBind方法返回的对象    private MyBinder myBinder = new MyBinder();    public class MyBinder extends Binder {        public int getCount() {            // 获取Service的运行状态:count            return count;        }    }    // 必须实现的方法,绑定Service时回调该方法    @Nullable    @Override    public IBinder onBind(Intent intent) {        System.out.println("Service is Binded");        return myBinder;    }    // Service创建时回调该方法    @Override    public void onCreate() {        super.onCreate();        System.out.println("Service is onCreate");        new Thread(){            @Override            public void run() {                super.run();                while (!quit) {                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    count++;                }            }        }.start();    }    // Service 被断开连接时回调该方法    @Override    public boolean onUnbind(Intent intent) {        System.out.println("Service is onUnbind");        return true;    }    // Service 被关闭之前回调该方法    @Override    public void onDestroy() {        super.onDestroy();        this.quit = true;        System.out.println("Service is onDestroy");    }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="shortcut.song.com.myapplication.BindServiceActivity">    <Button        android:id="@+id/bind_service"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="onBind"/>    <Button        android:id="@+id/unbind_service"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="unBind"/>    <Button        android:id="@+id/get_status"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Status"/></LinearLayout>
package shortcut.song.com.myapplication;import android.app.Service;import android.content.ComponentName;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.Button;import android.widget.Toast;public class BindServiceActivity extends AppCompatActivity {    Button bind,unbind,getStatus;    // 保持所启动的Service的IBinder对象    BindService.MyBinder myBinder;    // 定义一个ServiceConnection对象    private ServiceConnection conn = new ServiceConnection() {        // 当该Activity与Service连接成功时回调该方法        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            System.out.println("--Service connected--");            // 获取Service的onBind方法所返回的MyBinder对象;            myBinder = (BindService.MyBinder)service;        }        // 当该Activity与Service断开连接时回调该方法        @Override        public void onServiceDisconnected(ComponentName name) {            System.out.println("--Service Disconnected--");        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_bind_service);        bind = (Button)findViewById(R.id.bind_service);        unbind = (Button)findViewById(R.id.unbind_service);        getStatus = (Button)findViewById(R.id.get_status);        // 创建启动Service的Intent        final Intent intent = new Intent(this, BindService.class);        bind.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 绑定指定Service                bindService(intent, conn, Service.BIND_AUTO_CREATE);            }        });        unbind.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 解除绑定Service                unbindService(conn);            }        });        getStatus.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 获取并显示Service的count 值                Toast.makeText(BindServiceActivity.this, "Servicer 的值为:"+myBinder.getCount(), Toast.LENGTH_SHORT).show();            }        });    }}

这里写图片描述