下面列出了怎么用android.os.Handler.Callback的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Create a Handler with the given Looper
*
* @param async If true the Handler will calls {@link Message#setAsynchronous(boolean)} for
* each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
*/
public static Handler createHandler(Looper looper, boolean async) {
if (!async) {
return new Handler(looper);
}
// Standard way of exposing async handler on older api's from the support library
// https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/core
// /src/main/java/androidx/core/os/HandlerCompat.java#51
if (Build.VERSION.SDK_INT >= 28) {
return Handler.createAsync(looper);
}
if (Build.VERSION.SDK_INT >= 16) {
try {
//noinspection JavaReflectionMemberAccess
return Handler.class.getDeclaredConstructor(Looper.class, Callback.class, boolean.class)
.newInstance(looper, null, true);
} catch (Throwable ignored) {
}
}
return new Handler(looper);
}
@Override
public boolean handleMessage(Message msg) {
// proces listeners for specified type of message what
synchronized (mListenersSpecific) {
List<BetterWeakReference<Callback>> whatListofListeners = mListenersSpecific.get(msg.what);
if (whatListofListeners != null) {
handleListeners(whatListofListeners, msg);
if (whatListofListeners.size() == 0) {
mListenersSpecific.remove(msg.what);
}
}
}
// process universal listeners
handleListeners(mListenersUniversal, msg);
return true;
}
/**
* Register crash handler. It can catch ANR error automatically and use
* logger to output message.
*
* @param callback Call back object.When ANR error happened, it will use
* callback to notify developers. If it's null, it won't call
* back.
*/
public void registerCrashHandler(Callback callback)
{
if (application == null)
{
return;
}
AndroidCrashHandler.getInstance().setup(application, callback);
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = ResHelper.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = ResHelper.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public static Callback secure(Callback callback){
if(callback == null || callback instanceof SafeCallback){
return callback;
}
return new SafeCallback(callback);
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = ResHelper.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = ResHelper.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = R.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = R.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = ResHelper.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public static Callback secure(Callback callback){
if(callback == null || callback instanceof SafeCallback){
return callback;
}
return new SafeCallback(callback);
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = ResHelper.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
private static void handleMessage(Message msg) {
Object[] objs = (Object[]) msg.obj;
Message inner = (Message) objs[0];
Callback callback = (Callback) objs[1];
if (callback != null) {
callback.handleMessage(inner);
}
}
private static void handleMessage(Message msg) {
Object[] objs = (Object[]) msg.obj;
Message inner = (Message) objs[0];
Callback callback = (Callback) objs[1];
if (callback != null) {
callback.handleMessage(inner);
}
}
private static Message getMessage(int msgWhat, Callback callback) {
Message shell = new Message();
Message inner = new Message();
inner.what = msgWhat;
shell.obj = new Object[]{inner, callback};
return shell;
}
/**
* 发送消息
*
* @param callback
* @return
*/
public static boolean sendMessage(Callback callback, int arg1, int arg2, Object obj, int what, Bundle bundle) {
Message message = Message.obtain();
message.arg1 = arg1;
message.arg2 = arg2;
message.obj = obj;
message.what = what;
message.setData(bundle);
return handler.sendMessage(getShellMessage(message,callback));
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = R.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = R.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void onCancel(Platform plat, int action) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
activity.finish();
return false;
}
});
}
private void toast(final String resOrName) {
UIHandler.sendEmptyMessage(0, new Callback() {
public boolean handleMessage(Message msg) {
int resId = R.getStringRes(context, resOrName);
if (resId > 0) {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, resOrName, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}