展会信息港展会大全

Android中AIDL实现对Server的前台控制实例
来源:互联网   发布日期:2016-01-13 21:52:00   浏览:1466次  

导读:android系统进程间不能共享数据,需要提供一些机制,在server组建中主要是通过AIDL(Android Interferce Definition Language)实现的。其基本步骤如下:1在Eclipse中建立一个工程AIDLTest,建立一个扩展名为......

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支持有限的数据类型,在这些类型范围内都可进行想应的控制

赞助本站

人工智能实验室

相关热词: AIDL Server 前台 控制

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港