aidl android例子,android aidl使用小例子

本文介绍了一个Android应用中如何使用CompareService接口创建服务,包括onBind()方法的绑定、onUnbind()方法的解绑,以及在MainActivity中通过ServiceConnection实现服务的调用。重点展示了CompareToService类的实现和在主界面的交互过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件分布:

aab3c4a3ffcc53e04a632414211e7a3e.png

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

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" >

android:id="@+id/numberOne"

android:inputType="numberSigned"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="" />

android:id="@+id/numberTwo"

android:inputType="numberSigned"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/numberOne"

android:text="" />

android:id="@+id/linear"

android:layout_below="@+id/numberTwo"

android:layout_width="match_parent"

android:layout_height="50dp"

android:gravity="center">

android:id="@+id/binder"

android:onClick="binder"

android:layout_width="200dp"

android:layout_height="50dp"

android:text="@string/binderservice"/>

android:id="@+id/compareTo"

android:onClick="compareTo"

android:layout_width="200dp"

android:layout_height="50dp"

android:text="@string/app_name"/>

android:id="@+id/numberlarge"

android:inputType="numberSigned"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/linear"

android:text="" />

AndroidManifest.xml 服务添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值