`
ydbc
  • 浏览: 715915 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

我的Android进阶之旅------>Android Service学习之AIDL, Parcelable和远程服务————实例

 
阅读更多

通过上一篇(Android Service学习之AIDL, Parcelable和远程服务)的介绍,这一篇写一个小实例来实践一下

step1:建立两个应用,分别为RemoteService和RemoteServiceClient



先编写服务器端的内容

step2:开始编写一个StudentQuery.aidl文件

package cn.roco.aidl;

interface StudentQuery {
	String queryStudent(int number);
}

系统会根据此aidl文件生成一个类StudentQuery.java

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: C:\\Documents and Settings\\student\\android\\RemoteService\\src\\cn\\roco\\aidl\\StudentQuery.aidl
 */
package cn.roco.aidl;
public interface StudentQuery extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements cn.roco.aidl.StudentQuery
{
private static final java.lang.String DESCRIPTOR = "cn.roco.aidl.StudentQuery";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an cn.roco.aidl.StudentQuery interface,
 * generating a proxy if needed.
 */
public static cn.roco.aidl.StudentQuery asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof cn.roco.aidl.StudentQuery))) {
return ((cn.roco.aidl.StudentQuery)iin);
}
return new cn.roco.aidl.StudentQuery.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_queryStudent:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
java.lang.String _result = this.queryStudent(_arg0);
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements cn.roco.aidl.StudentQuery
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public java.lang.String queryStudent(int number) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(number);
mRemote.transact(Stub.TRANSACTION_queryStudent, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_queryStudent = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String queryStudent(int number) throws android.os.RemoteException;
}


step3:编写服务类StudentQueryService.java

package cn.roco.remote.service;

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

public class StudentQueryService extends Service {
	private String[] names = new String[100];
	private IBinder binder = new StudentQueryBinder();

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	/**
	 * 通过学号查询学生姓名
	 * 
	 * @param no
	 * @return
	 * @throws Exception
	 */
	public String query(int no) {
		if (no > 0 && no < names.length + 1) {
			return names[no - 1];
		} else {
			return null;
		}
	}

	/**
	 * 创建服务的时候做初始化操作
	 */
	@Override
	public void onCreate() {
		for (int i = 0; i < names.length; i++) {
			names[i] = "RemoteQuery_" + (i + 1);
		}
		super.onCreate();
	}

	/**
	 * 关闭服务的时候,释放资源
	 */
	@Override
	public void onDestroy() {
		for (int i = 0; i < names.length; i++) {
			names[i] = null;
		}
	}
	/**
	 * cn.roco.aidl.StudentQuery.Stub 由系统根据StudentQuery.aidl自动生成
	 */
	private final class StudentQueryBinder extends
			cn.roco.aidl.StudentQuery.Stub {
		@Override
		public String queryStudent(int number) {
			return query(number);
		}
	}
}

step4:在AndroidManifest.xml文件中配置此服务

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.roco.remote.service"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
		<service android:name=".StudentQueryService" >
			<intent-filter>
				<action android:name="cn.roco.remote.service.student.query"/>
			</intent-filter>
		</service>
    </application>
</manifest>


编写完服务器端的代码后,编写客户端的代码

step5:在客户端也要讲StudentQuery.aidl文件导入到客户端中


step6:设计客户端的UI界面,main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/student_no" />

	<EditText android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/student_no" />

	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/button_query"
		android:id="@+id/button_query" />
		
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/resultView" />

</LinearLayout>

string.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">远程学生查询系统</string>
    <string name="student_no">学号</string>
    <string name="button_query">查询</string>
</resources>


step7:MainActivity.java文件

package cn.roco.remoteservice.client;

import cn.roco.aidl.StudentQuery;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText student_no;
	private TextView resultView;
	private ServiceConnection conn = new StudentServiceConnection();
	private StudentQuery studentQuery;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		resultView = (TextView) findViewById(R.id.resultView);
		student_no = (EditText) findViewById(R.id.student_no);
		Button button_query = (Button) findViewById(R.id.button_query);
		button_query.setOnClickListener(new ButtonClickListener());
		/**
		 *  该Intent是RemoteService项目中的Service
		 *  "cn.roco.remote.service.student.query"为
		 *  <service android:name=".StudentQueryService" >
				<intent-filter>
					<action android:name="cn.roco.remote.service.student.query"/>
				</intent-filter>
			</service>
		 */
		Intent service = new Intent("cn.roco.remote.service.student.query");
		// 绑定服务
		bindService(service, conn, BIND_AUTO_CREATE);
	}

	@Override
	protected void onDestroy() {
		unbindService(conn); // 解绑服务
		super.onDestroy();
	}

	/**
	 * 该类中的两个方法都是回调函数
	 */
	private class StudentServiceConnection implements ServiceConnection {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// 调用该方法将远程的IBinder转换
			studentQuery = cn.roco.aidl.StudentQuery.Stub.asInterface(service);   //重点理解
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			studentQuery=null;
		}

	}

	private final class ButtonClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			String studentNo = student_no.getText().toString();
			String studentName;
			try {
				studentName = studentQuery.queryStudent(Integer
						.parseInt(studentNo));
				if (studentName!=null) {
					resultView.setText(studentName);
				}else{
					Toast.makeText(getApplicationContext(), "您输入的学号不在规定的学号内,请输入1到100的数字", 1).show();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}



step8:将服务和客户端安装完后,查看运行的效果





==================================================================================================

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics