安卓绑定本地服务Service并与之通信


效果

安卓绑定本地服务Service并与之通信_第1张图片

代码

1.主Activity

package com.example.test2.myapplication.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test2.myapplication.R;

/**
 * Created by Derek on 2017/2/18.
 * 绑定本地服务Service并与之通信
 */

public class BindServiceTest extends AppCompatActivity implements View.OnClickListener {

    private Button bind;
    private Button unbind;
    private Button getservicestatus;

    BindService.MyBinder binder;
    ServiceConnection serviceConnection = new ServiceConnection() {
        //Activityservice连接成功时,回调该方法
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d("GsonUtils", "onServiceConnected");
            binder = (BindService.MyBinder) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d("GsonUtils", "onServiceDisconnected");

        }
    };
    Intent intent;
    private TextView textView8;
    private TextView count;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bindservicetest);
        initView();
//        intent = new Intent();

        intent = new Intent(this, BindService.class);
        intent.setAction("com.example.test2.myapplication.service.Bind_Service");
    }

    private void initView() {
        bind = (Button) findViewById(R.id.bind);
        unbind = (Button) findViewById(R.id.unbind);
        getservicestatus = (Button) findViewById(R.id.getservicestatus);

        bind.setOnClickListener(this);
        unbind.setOnClickListener(this);
        getservicestatus.setOnClickListener(this);
        textView8 = (TextView) findViewById(R.id.textView8);
        textView8.setOnClickListener(this);
        count = (TextView) findViewById(R.id.count);
        count.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bind:
                //绑定Service
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                //解除绑定Service
                unbindService(serviceConnection);
                break;
            case R.id.getservicestatus:
                //不能直接设置int的值
                //count.setText(binder.getCount());
                //获取并显示Servicecount                count.setText("Servicecount值为:" +binder.getCount());
                Toast.makeText(this, "Servicecount值为:" + binder.getCount(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

2.服务

package com.example.test2.myapplication.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Derek on 2017/2/18.
 */

public class BindService extends Service {

    int count;
    boolean quit;

    //MyBinder通过继承Binder来实现IBinder    public class MyBinder extends Binder {
        public int getCount() {
            //获取service的运行状态值:count
            return count;
        }
    }

    //定义onBind方法所返回的对象
    private MyBinder binder = new MyBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("GsonUtils", "onBind");
        //返回IBinder对象
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //启动一条线程,动态的修改count的状态值
        new Thread() {
            @Override
            public void run() {
                super.run();
                while (!quit) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count++;
                }
            }
        }.start();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("GsonUtils", "onUnbind");
        return true;
    }

    //service被关闭时
    @Override
    public void onDestroy() {
        super.onDestroy();
        //停止线程任务的执行
        this.quit = true;
        Log.d("GsonUtils", "onDestroy");
    }
}

布局xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    

AndroidManifest.xml

android:name="com.example.test2.myapplication.service.BindService">
    
        android:name="com.example.test2.myapplication.service.Bind_Service">
    

。。。

你可能感兴趣的:(安卓service)