| |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| -> 移动开发 -> Android 小米手机劫持toast提示,内容被追加应用名称的解决方案 -> 正文阅读 |
|
|
[移动开发]Android 小米手机劫持toast提示,内容被追加应用名称的解决方案 |
背景:Android在开发过程中,经常使用toast消息提示。Toast为系统提供工具使用方法: Toast.make(Context context,String msg,int durcation).show(); 目前小米厂家的系统,已对toast底层做了修改,msg=appLabel+":"+msg,这种看似没问题,但是在插件开发中,如果资源出现错乱,Resid出现指向错误,就会导致,applabel显示异常。 分析: /**
* Show the view for the specified duration.
*/
public void show() {
if (mNextView == null) {
throw new RuntimeException("setView must have been called");
}
INotificationManager service = getService();
String pkg = mContext.getOpPackageName();
TN tn = mTN;
tn.mNextView = mNextView;
try {
service.enqueueToast(pkg, tn, mDuration);
} catch (RemoteException e) {
// Empty
}
}
Toast的show()方法,调用了INotificationManager,通过通知enqueueToast去提交信息。 mNextView为需要显示的view,如果我们通过hook,获取TN对象,获取到mNextView,把mNextView中的text修改成我们原有的?目前是可以实现的,一种是动态代理INotificationManager,另外就是不通过mNextView去做设置,直接通过Tost设置。 解决方案1:简单修改Toast,通过设置setext(String msg) public void setText(CharSequence s) {
if (mNextView == null) {
throw new RuntimeException("This Toast was not created with Toast.makeText()");
}
TextView tv = mNextView.findViewById(com.android.internal.R.id.message);
if (tv == null) {
throw new RuntimeException("This Toast was not created with Toast.makeText()");
}
tv.setText(s);
}
Toast toast=Toast.make(getapplication(),null,Toast.LENGTH_SHORT); toast.setText("你好"); 这种方法:public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
@NonNull CharSequence text, @Duration int duration) {
Toast result = new Toast(context, looper);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
还是内部创建一个临时的,然后通过mNextView创建一个视图,这样就绕开了TN这个环节,但是在minu12还是有问题,绕不开。 解决方案2:既然绕不开,那我们是否通过Hook,动态代理INotificationManager,对enqueueToast进行拦截? 证明是可行的。 INotificationManager是系统类,但是Toast中已定义了,可以通过反射拿到,
?2.2一个对象被使用,就要实例化。INotificationManager实例化,可以通过getService()来获取 Method ServiceMethod = toastClass.getDeclaredMethod("getService", new Class[0]);
获取到这个方法,我们需要进行调用: //invoke是反射中的方法调用,不懂的,可以先了解反射机制 final Object service = ServiceMethod .invoke(null); 这个时候我们就可以设置创建代理: Object proxy = Proxy.newProxyInstance(Thread.class.getClassLoader(), new Class[]{INotificationManager}, new InvocationHandler(){};
INotificationManager:是系统类,通过反射获取。 //service这个时候已被初始化了,静态类,设置代理 sServiceField.set(null, proxy); 接下:我们要处理代理获取的类 1. Toast通过TN的mNextView来完成消息的通过,所以先获取内部类 //内部类反射
Class<?> tnClass = Class.forName(Toast.class.getName() + "$TN");
// 获取mNextView的Field
Field mNextViewField = tnClass.getDeclaredField("mNextView");
// 获取mNextView实例,查看元布局 LinearLayout mNextView = (LinearLayout) mNextViewField.get(item); //获取布局下的子view,获取text的,这样就完成text的过滤和重新赋值 TextView childView = (TextView) mNextView.getChildAt(0); CharSequence text = childView.getText(); //动态代理: public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
?????????????//enqueueToast的是service发送通知消息的,通过这个方法来获取TN View
if (method.getName().equals("enqueueToast")) {
modifyMethod(args[1], context);
}
return method.invoke(service, args);
}
});
//返回当前service的代理
return sServiceField.set(null, proxy);
//************************************************************************** 完整代码如下:public class ToastInvokenManager {
public void init(final Context context) {
Class<Toast> toastClass = Toast.class;
try {
Field sServiceField = toastClass.getDeclaredField("sService");
sServiceField.setAccessible(true);
Class INotificationManager = Class.forName("android.app.INotificationManager");
Method getServiceMethod = toastClass.getDeclaredMethod("getService", new Class[0]);
getServiceMethod.setAccessible(true);
final Object service = getServiceMethod.invoke(null);
Object proxy = Proxy.newProxyInstance(Thread.class.getClassLoader(), new Class[]{INotificationManager}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("enqueueToast")) {
modifyMethod(args[1], context);
}
return method.invoke(service, args);
}
});
sServiceField.set(null, proxy);
} catch (Exception e) {
}
}
private void modifyMethod(Object item, Context context) {
if (item == null)
return;
// 获取TN的class
try {
//内部类反射
Class<?> tnClass = Class.forName(Toast.class.getName() + "$TN");
// 获取mNextView的Field
Field mNextViewField = tnClass.getDeclaredField("mNextView");
mNextViewField.setAccessible(true);
// 获取mNextView实例,查看元布局
LinearLayout mNextView = (LinearLayout) mNextViewField.get(item);
// 获取textview
if (mNextView != null && mNextView.getChildCount() > 0) {
TextView childView = (TextView) mNextView.getChildAt(0);
CharSequence text = childView.getText();
if (!TextUtils.isEmpty(text)) {
String appName = getAppLabel(context);
if (!TextUtils.isEmpty(appName) && text.toString().startsWith(appName)) {
int count = appName.length() + 1;
String val = text.toString().substring(count, text.length());
childView.setText(val);
childView.invalidate();
}
}
}
} catch (Exception e) {
Log.d("e=" + e.getMessage());
}
}
private String getAppLabel(Context context) {
PackageManager manager = context.getPackageManager();
try {
ApplicationInfo pkinfo = manager.getApplicationInfo(context.getPackageName(), 0);
if (pkinfo != null) {
return context.getResources().getString(pkinfo.labelRes) + "";
}
} catch (Exception e) {
}
return null;
}
}
//*********************************************************************** 在Application 初始化即可。 |
|
|
| 移动开发 最新文章 |
| Vue3装载axios和element-ui |
| android adb cmd |
| 【xcode】Xcode常用快捷键与技巧 |
| Android开发中的线程池使用 |
| Java 和 Android 的 Base64 |
| Android 测试文字编码格式 |
| 微信小程序支付 |
| 安卓权限记录 |
| 知乎之自动养号 |
| 【Android Jetpack】DataStore |
|
|
| 上一篇文章 下一篇文章 查看所有文章 |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| 360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年11日历 | -2025/11/4 4:44:07- |
|
| 网站联系: qq:121756557 email:121756557@qq.com IT数码 |