android aidl使用小例子

文件分布:android aidl使用小例子

CompareService.aidl

package com.anlen.comparetoservice;

interface CompareService {
	int  CompareTo(int num1,int num2);
}

CompareToService.java

package com.anlen.comparetoservice;

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

public class CompareToService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("成功绑定服务");
		return new CompareIBinder();
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("解除绑定服务");
		return super.onUnbind(intent);
	}
	@Override
	public void onCreate() {
		System.out.println("远程服务被创建");
		super.onCreate();
	}
	@Override
	public void onDestroy() {
		System.out.println("远程服务被销毁");
		super.onDestroy();
	}
	
	/*
	 * CompareToService内部比较函数
	 */
	private int compareTo(int num1,int num2){
		return num1>num2?num1:num2;
	}
	
	//内部类实现接口并调用service内部方法,使外部类也能调用service内的方法
	private class CompareIBinder extends CompareService.Stub{
		@Override
		public int CompareTo(int num1, int num2) throws RemoteException {
			return compareTo(num1, num2);
		}	

	
	}
}

MainActivity.java

package com.anlen.experimentthree;

import com.anlen.comparetoservice.CompareService;

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;

public class MainActivity extends Activity {
	Intent intent;
	EditText numberOne;
	EditText numberTwo;
	EditText numberLarge;
	
	
	private CompareService compareService;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		numberOne = (EditText)findViewById(R.id.numberOne);
		numberTwo = (EditText)findViewById(R.id.numberTwo);
		numberLarge = (EditText)findViewById(R.id.numberlarge);
		
		/*
		 * 为意图设定action
		 * */
		intent=new Intent();
		intent.setAction("com.anlen.CompareToService");
	
	}
	public void binder(View view){
		bindService(intent, new ConnectService(), BIND_AUTO_CREATE);
	}
	public void compareTo(View view){
		
		String numberone=numberOne.getText().toString();
		String numbertwo=numberTwo.getText().toString();
		
		int one = Integer.parseInt(numberone);
		int two = Integer.parseInt(numbertwo);
		
		int large = 0;
			try {
				large = compareService.CompareTo(one, two);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		numberLarge.setText(Integer.toString(large)+"比较大");
	}
	
	/*
	 * 连接服务类
	 * */
	private class ConnectService implements ServiceConnection  {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			//强制转化类型
			compareService=CompareService.Stub.asInterface(service);
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {
			compareService=null;
		}
	}

}

布局xml

<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/numberOne"
		android:inputType="numberSigned"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:text="" />
    <EditText
        android:id="@+id/numberTwo"
        android:inputType="numberSigned"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/numberOne"
        android:text="" />
   <LinearLayout
       android:id="@+id/linear"
       android:layout_below="@+id/numberTwo"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:gravity="center"> 
       <Button 
         android:id="@+id/binder"
         android:onClick="binder"
         android:layout_width="200dp"
         android:layout_height="50dp"
         android:text="@string/binderservice"/>
        <Button 
         android:id="@+id/compareTo"
         android:onClick="compareTo"
         android:layout_width="200dp"
         android:layout_height="50dp"
         android:text="@string/app_name"/>
     </LinearLayout>
      <EditText
      android:id="@+id/numberlarge"
      android:inputType="numberSigned"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/linear"
      android:text="" />
</RelativeLayout>

AndroidManifest.xml 服务添加

  
        <service  android:name="com.anlen.comparetoservice.CompareToService">
            <intent-filter>
                <!-- 设定一个名字给外部进程调用该服务 -->
               <action android:name="com.anlen.CompareToService" />
      		 </intent-filter>
       </service>


你可能感兴趣的:(android,aidl)