Android中AIDL实现对Server的前台控制实例

本文详细介绍使用AIDL(Android Interface Definition Language)在Android系统中实现进程间通信的过程。包括创建AIDL文件、服务类、配置文件及客户端工程的具体步骤。

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

android系统进程间不能共享数据,需要提供一些机制,在server组建中主要是通过AIDL(Android Interferce Definition Language)实现的。其基本步骤如下:

1在Eclipse中建立一个工程AIDLTest,建立一个扩展名为aidl的源文件,其中的语法与JAVA中略有不同:

package com.data;
interface IMyService{
String getValue();

}

如果文件建立正确,则在gen目录下生成相应的.java文件:

2建立一个服务类MyService,其中便是你想通过前台操作服务的方法所在地,代码如下:

package com.data;

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

public class MyService extends Service {
public class MyServiceImpl extends IMyService.Stub{    //这些是必须的

 public String getValue() throws RemoteException {
  // TODO Auto-generated method stub
  return "我的AIDL实验";
 }
 
 
}
 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return new MyServiceImpl();
 }

}

3在AndroidManifest.xml中配置MyService类如下:

 <service android:name=".MyService">
            <intent-filter>
                <action android:name="com.data.aidl.IMyService"/>
            </intent-filter>
        </service>

 

Server的结构代码如下:

4建立客户端的工程文件AIDLclient,将gen下自动生成的.java文件连同包一起拷贝到src文件夹下,在Main目录中实现Activity与后台service绑定:代码如下:

package com.data.client;

import com.data.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.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
 private IMyService myService=null;
 private Button btu_Inv_AIDLServer;
 private Button btu_Bind_AIDLService;
 private TextView textView;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btu_Bind_AIDLService=(Button)findViewById(R.id.btu_Bind_AIDLService);
        btu_Inv_AIDLServer=(Button)findViewById(R.id.btu_Inv_AIDLServer);
        btu_Inv_AIDLServer.setEnabled(false);
        TextView textView=(TextView)findViewById(R.id.textview);
        btu_Inv_AIDLServer.setOnClickListener(this);
        btu_Bind_AIDLService.setOnClickListener(this);
    }

 private ServiceConnection serviceConnection=new ServiceConnection() {
  
  public void onServiceDisconnected(ComponentName name) {
   // TODO Auto-generated method stub
   
  }
  
  public void onServiceConnected(ComponentName name, IBinder service) {
   // TODO Auto-generated method stub
   //获得服务对象
   myService=IMyService.Stub.asInterface(service);
   btu_Inv_AIDLServer.setEnabled(true);
  }
 };
    public void onClick(View v) {
  // TODO Auto-generated method stub
  switch (v.getId()) {
  case R.id.btu_Bind_AIDLService:
   bindService(new Intent("com.data.aidl.IMyService"), serviceConnection, Context.BIND_AUTO_CREATE);
   break;

  case R.id.btu_Inv_AIDLServer:
   try {
    textView.setText(myService.getValue());
   } catch (Exception e) {
    // TODO: handle exception
   }
   break;
  }
 }
}

工程结构如下:

Activity控制界面如下

这样就实现了对后台服务的控制,AIDL支持有限的数据类型,在这些类型范围内都可进行想应的控制。

转载于:https://my.oschina.net/haquanwen/blog/55461

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值