[android] 05_绑定服务并调用服务内的方法

Android 4.0

绑定服务并调用服务内的方法

1、布局 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="绑定服务" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="call"
        android:text="调用服务内的方法" />
</LinearLayout>
2、定义一个类继承Service类
package cn.zengfansheng.callMethodInService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
    @Override
    public void onCreate() {
        System.out.println("服务被创建了。。。");
        super.onCreate();
    }
    @Override
    public void onDestroy() {
        System.out.println("服务被销毁了。。。");
        super.onDestroy();
    }
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("服务被绑定了。。。");
        return null;
    }
}
3、在AndroidManifest.xml中<application>节点下注册Service
<service android:name="cn.zengfansheng.callMethodInService.MyService" >
</service>
4、编写Service中的对外暴露的方法,及返回的 IBinder(对外暴露IService接口)
package cn.zengfansheng.callMethodInService;
public interface IService {
    /**
     * Service中对外暴露的方法
     */
    public void callMethodInService();
}
--------------------------------------------------------
package cn.zengfansheng.callMethodInService;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
    @Override
    public void onCreate() {
        System.out.println("服务被创建了。。。");
        super.onCreate();
    }
    @Override
    public void onDestroy() {
        System.out.println("服务被销毁了。。。");
        super.onDestroy();
    }
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("服务被绑定了。。。");
        return new MyBinder();
    }
    private class MyBinder extends Binder implements IService {
        @Override
        public void callMethodInService() {
            methodInService();
        }
    }
    public void methodInService() {
        System.err.println("我是一个在Service里面的方法。。。");
    }
}
五、绑定Service,定义一个类MyConn实现ServiceConnection接口,实现其中的方法
public void onServiceConnected(ComponentName name, IBinder service),其中的service就是该Activity和Service的代理人,可以通过该对象的引用间接地调用Service内的方法。
package cn.zengfansheng.callMethodInService;
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;
public class MainActivity extends Activity {
    private ServiceConnection conn;
    private IService iService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**
     * 绑定服务
     * @param view
     */
    public void bind(View view) {
        Intent service = new Intent(this, MyService.class);
        conn = new MyConn();
        this.bindService(service, conn, Context.BIND_AUTO_CREATE);
    }
    private class MyConn implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("绑定成功了。。。");
            iService = (IService) service;
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }
    /**
     * 调用服务内的方法
     * 
     * @param view
     */
    public void call(View view) {
        iService.callMethodInService();// 间接地调用了Service中的方法
    }
}
完整源码: