不采用AIDL文件的service
来源:互联网 发布:pp助手网络连接失败 编辑:程序博客网 时间:2024/06/02 05:04
服务:
package com.example.test3;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
public class MyService extends Service {
private static final java.lang.String DESCRIPTOR = "com.example.test3.aa";
static final int TRANSACTION_ss = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
private static final String TAG = "MyService3";
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind");
return new MyBinder();
}
class MyBinder extends Binder {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_ss: {
data.enforceInterface(DESCRIPTOR);
reply.writeNoException();
int i = data.readInt();
Log.e(TAG, "MyService3 " + i);
reply.writeInt(i);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
}
}
客户端:
package com.example.test4;
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.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
public class Client extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
aa mProxy ;
private static final java.lang.String DESCRIPTOR = "com.example.test3.aa";
static final int TRANSACTION_ss = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
this.findViewById(R.id.button1).setOnClickListener(this);
Intent service = new Intent("com.example.test3.MyService2");
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder obj) {
mProxy =new Proxy(obj);
}
};
this.bindService(service, conn, Context.BIND_AUTO_CREATE);
}
private static class Proxy implements aa {
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote) {
mRemote = remote;
}
public int ss(int i) {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result=-1;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(i);
mRemote.transact(TRANSACTION_ss, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
} catch (RemoteException e) {
e.printStackTrace();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
@Override
public void onClick(View v) {
Log.e(TAG, "" + mProxy.ss(752));
}
}
重要: Binder.onTransact IBinder.transact
- 不采用AIDL文件的service
- Android中不使用AIDL实现Service的远程调用
- Android中不使用AIDL实现Service的远程调用
- AIDL Service 的使用
- service中AIDL的应用
- service和aidl的区别
- 采用aidl技术实现电话的挂断
- Service AIDL
- service aidl
- service、aidl
- AIDL Service
- Service--AIDL
- Service, AIDL
- 文件aidl的使用方法
- Android Service 的使用(五)----如何实现AIDL文件中含有对象的调用
- service通讯的序列化 AIDL
- Android远程service aidl的用法
- AIDL和Service的区别是什么
- css postion的两种属性学习 absolute 和 relative
- mysql问题Can't connect to local MySQL server through socket '/usr/local/mysql/var/mysql.sock'
- 两道递归/回溯好题的分析:printSquares,maxSum
- 寫給大學生的程式技能 Cheatsheets
- Linux命令缩写含义?看全称,扫盲!
- 不采用AIDL文件的service
- sgu 155 Cartesian Tree (poj2201) 笛卡尔树构造
- 两表关联修改SQL
- Netty 4.0 新的特性及需要注意的地方
- struts2 session不一致问题
- lua和C/C++语言之间的调用
- 软件工程——面向过程的软件设计方法
- 待完成
- 黑马之行,正式启动!