1. 程式人生 > >android aidl通訊兩個APP之間的應用

android aidl通訊兩個APP之間的應用

一:資料提供端程式碼(資料提供APP)

1.java程式碼:Service

package com.example.remoteaidi;

import com.example.remoteaidi.aidl.StudentAidl;

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

public class StudentService extends Service {
	private String[] studentNo = {"王1","2","王3","王4","王月4","宋6"};
	
	private IBinder binder = new StudentQueryBinder();
	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
	
	class StudentQueryBinder extends StudentAidl.Stub {

		@Override
		public String getStudent(int no) throws RemoteException {
			int l = studentNo.length;
			if(l<0) {
				no = 0;
			}
			if(no>=l) {
				no = l - 1;
			}
			return studentNo[no];
		}
		
	}
	

}
aidl檔案
package com.example.remoteaidi.aidl;

interface StudentAidl {
	String getStudent(int no);
}

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
       <service android:name=".StudentService">
           <intent-filter>
               <action android:name="student.query"/>
               <category android:name="android.intent.category.DEFAULT"></category>
           </intent-filter>
       </service>
    </application>

</manifest>

二:請求端程式碼
package com.example.aidlclient;

import com.example.remoteaidi.aidl.StudentAidl;

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

public class MainActivity extends Activity {

	private ServiceConnectionImpl sci ;
	private StudentAidl student;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent = new Intent();	
		intent.setAction("student.query");
		sci = new ServiceConnectionImpl();
		bindService(intent, sci, BIND_AUTO_CREATE);
	}
	
	public void queryOnClick(View v) {
		EditText et_no_text = (EditText) findViewById(R.id.et_no_text);
		String no = et_no_text.getText().toString();
		String sname;
		try {
			sname = student.getStudent(new Integer(no));
			TextView tv_no = (TextView) findViewById(R.id.tv_no);
			tv_no.setText(sname);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	class ServiceConnectionImpl implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			student = StudentAidl.Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			student = null;
		}
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(sci != null) {
			unbindService(sci);
		}
	}

	

}

aidl檔案與上一個aidl檔案相同
package com.example.remoteaidi.aidl;

interface StudentAidl {
	String getStudent(int no);
}
佈局檔案
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_no_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn_query"  
        android:onClick="queryOnClick"
        android:text="查詢"
        android:layout_below="@id/et_no_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView  
        android:id="@+id/tv_no"  
        android:layout_below="@id/btn_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

</manifest>