1. 程式人生 > >Android-bindService本地服務-初步-Service返回物件

Android-bindService本地服務-初步-Service返回物件

在Android開發過程中,Android API 已經有了startService方式,為什麼還需要bindService呢?

答:是因為bindService可以實現Activity-->Service裡面的方法並返回資料給Activity,可以理解 在自身APP過程中Service與Activity進行通訊(本地服務)

  bindService還可以實現另外強大的功能,需要結合AIDL,實現跨程序Service與Activity進行通訊(遠端服務)

 

定義介面來增強服務返回到Binder:

package liudeli.service1.service.inter;

import liudeli.service1.service.domain.Person; public interface IPerson { /** * 通過ID查詢Person物件 * @param id */ public Person queryPersonByID(int id); }

 

服務處理onBind方法:

package liudeli.service1.service;

import android.app.Service;

import android.content.Intent;
import
android.os.Binder; import android.os.IBinder; import liudeli.service1.service.domain.Person; import liudeli.service1.service.inter.IPerson; public class MyService3 extends Service { @Override public IBinder onBind(Intent intent) { return new MyBinder(); } class MyBinder extends
Binder implements IPerson { /** * 通過ID查詢Person物件 * * @param id */ @Override public Person queryPersonByID(int id) { Person person = null; switch (id) { case 1: person = new Person(1, "張三", 30); break; case 2: person = new Person(2, "李四", 20); break; case 3: person = new Person(3, "趙六", 26); break; default: person = new Person(9,"王五", 40); break; } return person; } } }

 

MainActivity3呼叫服務到方法:

package liudeli.service1;

import android.app.Activity;

import android.content.ComponentName;
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 liudeli.service1.service.MyService3;
import liudeli.service1.service.inter.IPerson;

public class MainActivity3 extends Activity {

    private final static String TAG = MainActivity3.class.getSimpleName();

    private MyServiceConnection connection = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        connection = new MyServiceConnection();
    }

    public void bindService(View view) {
        bindService(new Intent(this, MyService3.class), connection, BIND_AUTO_CREATE);
    }

    public void unBindService(View view) {
        unbindService(connection);
    }

    private IPerson person = null;

    /**
     * 服務連線介面具體實現物件
     */
    class MyServiceConnection implements ServiceConnection {

        /**
         * 與服務進行連線
         * @param name
         * @param service
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("MyService3", "與服務進行連線 name:" + name);

            person = (IPerson) service;
        }

        /**
         * 與服務斷開連線
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            person = null;
            System.gc();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != connection) {
            unbindService(connection);
        }
    }

    public void testMothod(View view) {
        if (null != person) {
            liudeli.service1.service.domain.Person p = person.queryPersonByID(1);
            Log.d("MyService3", "p.toString" + p.toString());
        }
    }
}