package com.zlc.aidl.client;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import android.util.Log;
public class ReflectionInvoke {
public static HashMap> reflections = new HashMap>();
private Method method;
private Class> clazz;
private Class> parameterClasses[];
static class InvokeKey {
String className;
String methodName;
String[] paramsClassName;
public InvokeKey(String className, String methodName, String[] paramsClassName) {
this.className = className;
this.methodName = methodName;
this.paramsClassName = paramsClassName;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
int ret = className.hashCode();
ret = methodName.hashCode() + ret * 10;
for (int i = 0; i < paramsClassName.length; i++) {
ret = paramsClassName[i].hashCode() + ret * 10;
}
return ret;
}
@Override
public boolean equals(Object o) {
if (o instanceof InvokeKey) {
InvokeKey key = (InvokeKey) o;
if (!key.className.equals(className) || !key.methodName.equals(methodName)
|| key.paramsClassName.length != paramsClassName.length) {
return false;
} else {
for (int i = 0; i < paramsClassName.length; i++) {
if (!key.paramsClassName[i].equals(paramsClassName[i]))
return false;
}
return true;
}
}
return false;
}
}
Class> classForNameX(String name) throws Exception {
if ("int".equals(name))
return int.class;
else if ("char".equals(name))
return char.class;
else if ("short".equals(name))
return short.class;
else if ("byte".equals(name))
return byte.class;
else if ("long".equals(name))
return long.class;
else if ("float".equals(name))
return float.class;
else if ("double".equals(name))
return double.class;
else if ("boolean".equals(name))
return boolean.class;
return Class.forName(name);
}
private ReflectionInvoke(InvokeKey key) throws Exception {
clazz = Class.forName(key.className);
parameterClasses = new Class[key.paramsClassName.length];
for (int i = 0; i < parameterClasses.length; i++) {
parameterClasses[i] = classForNameX(key.paramsClassName[i]);
}
method = clazz.getMethod(key.methodName, parameterClasses);
}
public static ReflectionInvoke getInvoker(String className, String methodName,
List argsClassName) {
String[] arrayarges = new String[argsClassName.size()];
argsClassName.toArray(arrayarges);
return getInvoker(className, methodName, arrayarges);
}
public static ReflectionInvoke getInvoker(String className, String methodName, String... args) {
ReflectionInvoke invoke = null;
synchronized (reflections) {
try {
InvokeKey key = new InvokeKey(className, methodName, args);
WeakReference w = reflections.get(key);
if (w == null ? true : (invoke = w.get()) == null) {
invoke = new ReflectionInvoke(key);
reflections.put(key, new WeakReference(invoke));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return invoke;
}
public Object invokeNoException(Object obj, Object... args) {
try {
return method.invoke(obj, args);
} catch (Exception e) {
Log.d("Exception", "invoke error");
}
return null;
}
public Object invoke(Object obj, Object... args) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
Log.d("ReflectionInvoke", "invoke invoke invoke");
return method.invoke(obj, args);
}
}
其次:需要准备一个实现parcelable的对象,用来在两个进程之间传递方法名、方法参数等信息。
package com.zlc.aidl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
public class JsonReflectionInvokParcelable implements Parcelable{
public String objectId;
public String methodname;
public List argsClassName;
public List
服务端调用
再次:服务端发送数据之前检测并且封装参数的类型
……
public void callReflection(String json, String objId, String method,
Object ...args) throws RemoteException {
JsonReflectionInvokParcelable p = new JsonReflectionInvokParcelable();
p.objectId = objId;
p.methodname = method;
p.argsClassName = new ArrayList();
p.argsVaule = new ArrayList();
Log.d(TAG, "callReflection222 json=" + json);
for (int i = 0; i < args.length; i++) {
Object o = args[i];
@SuppressWarnings("rawtypes")
Class clazz = o.getClass();
if (clazz.isPrimitive() || o instanceof Parcelable || o instanceof String
|| o instanceof Integer || o instanceof Short || o instanceof Float
|| o instanceof Double || o instanceof Character) {
p.argsClassName.add(clazz.getCanonicalName());
p.argsVaule.add(o);
} else {
throw new RuntimeException("args only support primitives and String");
}
}
callback.onReflectionCallback(p, json);
Log.d(TAG, "callReflection222 end json=" + json);
}
……
最后:从服务端调用客户端注册view的某个指定方法
private final IMyAidlService.Stub mBinder = new IMyAidlService.Stub() {
@Override
public void registerClient(AIDLCallback cb) throws RemoteException {
Log.d(TAG, "registerClient");
callback = cb;
Bitmap bmp = BitmapFactory.decodeFile("/data/data/com.zlc.aidl.server/files/1.jpg");
callReflection("just for test","myImageView","setImageBitmap",bmp);
cb.asBinder().linkToDeath(new DeathRecipient() {
@Override
public void binderDied() {
// TODO Auto-generated method stub
try {
Log.i(TAG, "[ServiceAIDLImpl]binderDied.");
} catch (Throwable e) {
}
}
}, 0);
}
};
FineReport使用中遇到的常见报错及解决办法(一)
这里写点抛砖引玉,希望大家能把自己整理的问题及解决方法晾出来,Mark一下,利人利己。
出现问题先搜一下文档上有没有,再看看度娘有没有,再看看论坛有没有。有报错要看日志。下面简单罗列下常见的问题,大多文档上都有提到的。
1、address pool is full:
含义:地址池满,连接数超过并发数上
原文:http://kindlefireforkid.com/how-to-setup-a-google-account-on-amazon-fire-tablet/
Step 4: Run ADB command from your PC
On the PC, you need install Amazon Fire ADB driver and instal
本文译者:candeladiao,原文:URL filtering for UIWebView on the iPhone说明:译者在做app开发时,因为页面的javascript文件比较大导致加载速度很慢,所以想把javascript文件打包在app里,当UIWebView需要加载该脚本时就从app本地读取,但UIWebView并不支持加载本地资源。最后从下文中找到了解决方法,第一次翻译,难免有