Android進階—進程間通信 AIDL Service

Android進程間通信 AIDL Service

一、服務端結構




服務端需要處理的有:

1.處理對象,對象內方法,注意:AIDL 只能對函數起作用;

package com.example.aidl;  
interface Person {  
    String getUserName(String someone);  
}  


2.處理服務,在服務中綁定(onBind)和解綁(onUnbind)以及其他生命週期的方法中處理

package com.example.service;

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

import com.example.aidl.Person;

public class AIDLService extends Service {
	private static final String TAG ="AIDLService";
	
	//處理對象數據,邏輯控制等操作
	Person.Stub personStub= new Person.Stub() {
		
		@Override
		public String getUserName(String someone) throws RemoteException {
			Log.i(TAG, "getUserName");
			return "getUserName:"+someone;
		}
	};
	// 方法中切記 ,返回的一定是  Binder 對象
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "onBind");
		return personStub;
	}
	@Override
	public boolean onUnbind(Intent intent) {
		Log.i(TAG, "onUnbind");
		return super.onUnbind(intent);
	}
	
	@Override
	public void onDestroy() {
		Log.i(TAG, "onDestroy");
		super.onDestroy();
	}
}


3.配置文件(android:exported="true"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.aidldemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
	<permission android:protectionLevel="normal" android:name="permission.com.example.service.AIDLService"></permission>
    <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="com.example.service.AIDLService"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.AIDLService" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
    </application>

</manifest>

二、客戶端 配置




1.對象,切記,與服務端的對象完全一致,包括包名

2.調用

package com.example.aidlclient;

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

import com.example.aidl.Person;

public class MainActivity extends Activity implements OnClickListener{

	private Person person;
	private ServiceConnection connection= new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
		
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			person = Person.Stub.asInterface(service);
			
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.btn1).setOnClickListener(this);
		findViewById(R.id.btn2).setOnClickListener(this);
		findViewById(R.id.btn3).setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.btn1)
		{
			Intent intent= new Intent("android.intent.action.AIDLService");
			bindService(intent, connection, Context.BIND_AUTO_CREATE);
			
		}else if(v.getId()==R.id.btn2)
		{
			try {
				Log.i("onClick", person.getUserName("qubian"));
			} catch (RemoteException e) {

				e.printStackTrace();
			}
		}else if(v.getId()==R.id.btn3)
		{
			unbindService(connection);
		}
			
		
	}
}


下載地址:

http://download.csdn.net/detail/naibbian/8719687

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章