1. 程式人生 > >Android -- 跨應用繫結service並通訊

Android -- 跨應用繫結service並通訊

第一步,
需要修改service1專案中aidl,增加一個方法。

package com.example.service1.aidl;  

interface IMyService {  

    void basicType();

    void setName(String name);
}

setName用於儲存name的方法。
然後clear專案

第二步,
此時,我們service類中的onBind方法需要實現新介面。

package com.example.service1;

import android.app.Service;
import android.content.Intent;
import
android.os.IBinder; import android.os.RemoteException; import com.example.service1.aidl.IMyService; public class MyService extends Service { private String serviceName = "預設名字"; private boolean running; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub
return new IMyService.Stub() { @Override public void basicType() throws RemoteException { // TODO Auto-generated method stub } @Override public void setName(String name) throws RemoteException { serviceName = name; } }; } @Override
public void onCreate() { running = true; new Thread() { public void run() { while (running) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(serviceName); } }; }.start(); super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); running = false; } }

程式碼27到29行,接收到name並且放入全域性變數中,提供給onCreate方法輸出。

第三步,
將service1專案中aidl拷貝到service2專案中,並且包名要一致,
這裡寫圖片描述

第四步,
修改service2應用activity佈局,增加一個text域,和一個按鈕。用於將text中的資訊提交到service1專案的service中。
這裡寫圖片描述

第五步,
修改service2專案中activity,增加與service1的通訊,

package com.example.service2;

import com.example.service1.aidl.IMyService;

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.os.RemoteException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener,
        ServiceConnection {

    Intent serviceIntent;

    private IMyService imyService = null;

    TextView t;

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

        serviceIntent = new Intent();
        serviceIntent.setComponent(new ComponentName("com.example.service1",
                "com.example.service1.MyService"));

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);

        findViewById(R.id.button5).setOnClickListener(this);

        t = (TextView) findViewById(R.id.textView1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            startService(serviceIntent);
            break;
        case R.id.button2:
            stopService(serviceIntent);
            break;
        case R.id.button3:
            bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
            break;
        case R.id.button4:
            unbindService(this);
            imyService = null;
            break;
        case R.id.button5:
            if (imyService != null) {
                try {
                    imyService.setName(t.getText().toString());
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        imyService = IMyService.Stub.asInterface(service);
        System.out.println("onServiceConnected");
        System.out.println(service);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }

}

程式碼72行,傳輸資料
程式碼84行用法imyService = IMyService.Stub.asInterface(service);

執行結果,如圖,
這裡寫圖片描述