23 服务的绑定启动Demo3

这里写图片描述

MainActivity.java

package com.example.day23_service_demo3;

import com.example.day23_service_demo3.MyService.MyBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    private MyServiceConn conn;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        conn = new MyServiceConn();
        tv = (TextView) findViewById(R.id.tv);
    }

    //绑定服务
    public void MyBindServiceClick(View v){

        Intent intent = new Intent(MainActivity.this, MyService.class);

        /**
         * 参数1:intent对象  指明绑定的组件
         * 参数2:当前服务的链接状态
         * 参数3:绑定服务的标记   :绑定且创建
         */
        bindService(intent, conn, Context.BIND_AUTO_CREATE);

    }
    //解除绑定服务
    public void MyUnBindServiceClick(View v){
        unbindService(conn);
    }

    /**
     * 创建服务的链接对象 MyServiceConn
     * @author sxy
     *
     */
    class MyServiceConn implements ServiceConnection{

        /**
         * 表示当前服务连接成功后回调此方法
         * 参数1:组件名称
         * 参数2:连接后onBind() 返回的对象
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            MyBinder myBinder = (MyBinder) service;
            //获取到Service对象
            MyService myService = myBinder.getService();
            int num = myService.getRandom();

            tv.setText(num+"");
        }

        /**
         * 失去连接回调此方法
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            conn = null;
        }

    }

}

MyService.java

package com.example.day23_service_demo3;

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{

    private  static final String TAG ="MyService";

    /**
     * 必须实现的方法   绑定服务时调用的方法
     */
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub

        Log.e(TAG , "===onBind===");
        return new MyBinder();
    }

    //创建MyBinder类  将MyBinder传递给 绑定源   Service 不能直接去new 通过传递才可以
    public class MyBinder extends Binder{
        public MyService getService(){
            return MyService.this;

        }
    } 

    /**
     * 随机数
     * @return
     */
    public int getRandom(){
        return (int) (Math.random()*10+1);

    }



    /**
     * 表示服务创建时调用
     */
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.e(TAG , "===onCreate===");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub


        Log.e(TAG , "===onStartCommand===");

        return START_NOT_STICKY;
    }


    /**
     * 解除绑定时 调用此方法
     */
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Log.e(TAG , "===onUnbind===");
        return super.onUnbind(intent);
    }

    /**
     * Service销毁时调用此方法
     */
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        Log.e(TAG , "===onDestroy===");
    }

}

清单文件

AndroidManifest.xml

package com.example.day23_service_demo3;

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{

    private  static final String TAG ="MyService";

    /**
     * 必须实现的方法   绑定服务时调用的方法
     */
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub

        Log.e(TAG , "===onBind===");
        return new MyBinder();
    }

    //创建MyBinder类  将MyBinder传递给 绑定源   Service 不能直接去new 通过传递才可以
    public class MyBinder extends Binder{
        public MyService getService(){
            return MyService.this;

        }
    } 

    /**
     * 随机数
     * @return
     */
    public int getRandom(){
        return (int) (Math.random()*10+1);

    }



    /**
     * 表示服务创建时调用
     */
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.e(TAG , "===onCreate===");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub


        Log.e(TAG , "===onStartCommand===");

        return START_NOT_STICKY;
    }


    /**
     * 解除绑定时 调用此方法
     */
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Log.e(TAG , "===onUnbind===");
        return super.onUnbind(intent);
    }

    /**
     * Service销毁时调用此方法
     */
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        Log.e(TAG , "===onDestroy===");
    }

}

posted on 2016-09-22 22:28  木鱼哥  阅读(119)  评论(0编辑  收藏  举报

导航