maoruibin / maoruibin/maoruibin.github.com
Hook Toast,更改内部 Handler 的实现
Open
Nobody has claimed this yet.
blog
- Dominant language
- HTML
- Stars
- 22
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
核心代码
private void hookToast(Toast toast) {
try {
Field fieldTN = Toast.class.getDeclaredField("mTN");
fieldTN.setAccessible(true);
Field filedHandler = fieldTN.getType().getDeclaredField("mHandler");
filedHandler.setAccessible(true);
Object tn = fieldTN.get(toast);
Handler preHandler = (Handler) filedHandler.get(tn);
filedHandler.set(tn,new ToastHandlerWarp(preHandler));
} catch (Exception e) {
e.printStackTrace();
}
}
ToastHandlerWarp.java
public class ToastHandlerWarp extends Handler{
private static final String TAG = "ToastHandlerWarp";
private Handler mOriginHandler;
public ToastHandlerWarp(Handler preHandler) {
this.mOriginHandler = preHandler;
}
@Override
public void dispatchMessage(Message msg) {
try {
Log.d(TAG,"execute ToastHandlerWarp dispatchMessage");
mOriginHandler.dispatchMessage(msg);
}catch (Exception e){
// ignore
}
}
@Override
public void handleMessage(Message msg) {
Log.d(TAG,"execute ToastHandlerWarp handleMessage");
mOriginHandler.handleMessage(msg);
}
}
使用
Toast toast = Toast.makeText(v.getContext(), "测试Crash", Toast.LENGTH_SHORT);
hookToast(toast);
toast.show();
参考
逻辑分析见以下两篇不错的文章
简书一篇不错的讲解 Hook 的文章
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the hookToast snippet and ToastHandlerWarp.java, then compare the referenced Toast and Android Hook analyses with the shown usage. Done requires a specific target for the internal Handler change and a way to validate the Toast example; the issue names no target file or test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- mobile-dev
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100