绑定Service实例

package com.example.bindservice;

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.util.Log;
import android.view.View;
import android.widget.Button;

//使用绑定Service时,回调方法顺序为onCreate--onBind--onUnBind--onDestroy;

/** * @author HD * @date 2015-11-18 * @package_name com.example.bindservice * @file_name MainActivity.java */
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    public MyService myService;
    private Button  mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.button1);
        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
// 绑定service
                bindService(intent, conn, Context.BIND_AUTO_CREATE);
            }
        });
    }

// 匿名内部类
    private ServiceConnection conn = new ServiceConnection() {

// 当service连接失败时候调用此方法
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO 自动生成的方法存根
            Log.i(TAG, "----->>onServiceDisconnected");
            myService = null;
        }

// 当service连接成功时候调用此方法,在Service中调用onBind方法时,返回一个ibinder对象到此方法中
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO 自动生成的方法存根
// 调用在内部类ServiceBinder中的getService方法;
            myService = ((MyService.ServiceBinder) service).getService();
            Log.i(TAG, "----->>onServiceConnected");
        }
    };
}
package com.example.bindservice;

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


/** * @author HD * @date 2015-11-18 * @package_name com.example.bindservice * @file_name MyService.java */
public class MyService extends Service {
    private static final String TAG = "MyService";
    boolean threadDisable;
    int count;

    public MyService() {
        // TODO 自动生成的构造函数存根
    }

    @Override
    public void onCreate() {
        // TODO 自动生成的方法存根
        super.onCreate();
        Log.i(TAG, "------>>onCreate");
// 在新的线程中启动count++;
        new Thread(new Runnable() {
            public void run() {
                while (!threadDisable) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    count++;
                    Log.v("CountService", "Count is" + count);
                }
            }
        }).start();
    }

// 返回一个实现了IBinder接口的对象;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO 自动生成的方法存根
        Log.i(TAG, "------>>onBind");
        ServiceBinder binder = new ServiceBinder();
        return binder;
    }

// 当解除绑定时候调用这个方法;
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO 自动生成的方法存根
        Log.i(TAG, "------>>onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        // TODO 自动生成的方法存根
        Log.i(TAG, "------>>onDestroy");
        super.onDestroy();
        this.threadDisable = true;
    }

// 内部类,作为一个Service与Activity的接口
    class ServiceBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bindservice" android:versionCode="1" android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="21" />

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <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>

**

你可能感兴趣的:(绑定Service实例)