Service 绑定方式启动,生命周期。绑定方式读取服务器数据

package com.example.lenovo.service;

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

public class MyService extends Service {
    public MyService() {
        Log.e("TAG","MyService被创建");
    }

    @Override
    public void onCreate() {
        Log.e("TAG","onCreate被调用");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.e("TAG","onDestroy被调用");
        super.onDestroy();
    }

    @Override
    public void onRebind(Intent intent) {
        Log.e("TAG","onRebind被调用");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG","onUnbind被调用");
        return super.onUnbind(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String string = intent.getStringExtra("test");
        Log.e("TAG","onStartCommand被调用,并收到数据="+string);
        return super.onStartCommand(intent, flags, startId);
    }

    //代理对象
    public class MyBinder extends Binder
    {
        //定义数据交换的方法
        public int getTest()
        {
            return 123;
        }

    }

    int anInt;
    //回调方法
    //绑定
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("TAG","onBind被调用");
       //启动业务逻辑
        new Thread()
        {
            @Override
            public void run() {
                for(int i = 0;i<100;i++)
                {
                    anInt++;
                    try {
                        Thread.sleep(1000);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        //代理对象
        return new MyBinder();
    }
}
MyService
 ServiceConnection sc;
    MyService.MyBinder myb;
    //绑定
    public void bt_3(View v)
    {
        //以绑定方式启动
        //准备Intent:显式意图
        Intent intent = new Intent(this,MyService.class);
        if (sc==null) {
            sc = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {

                    //代理对象
                    myb = (MyService.MyBinder) service;
                    Toast.makeText(MainActivity.this, "绑定启动完成,接收返回的对象" + myb, Toast.LENGTH_SHORT).show();
                }

                //异常状态时触发
                @Override
                public void onServiceDisconnected(ComponentName name) {

                    Toast.makeText(MainActivity.this, "服务连接中断", Toast.LENGTH_SHORT).show();
                }
            };
        }
        //三个参数
        //1-意图
        //2-服务连接的实现类
        //3-启动方式,一般用Context.BIND_AUTO_CREATE
        bindService(intent, sc, Context.BIND_AUTO_CREATE);
    }
    //解除绑定
    public void bt_4(View v)
    {

        if (sc!=null) {
            unbindService(sc);
            sc=null;
        }
        else
        {
            Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
        }
    }
    //读取
    public void bt_5(View v)
    {
        //读取服务的运行数据
        //用代理对象
        Toast.makeText(MainActivity.this, "读到的服务数据="+myb.getTest(), Toast.LENGTH_SHORT).show();
    }
MainActivity.java

原文地址:https://www.cnblogs.com/1ming/p/5620353.html