-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWechatUtils.java
105 lines (80 loc) · 3.12 KB
/
WechatUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.example.admin.registerwxdemo.Utils;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import java.io.IOException;
import java.util.List;
import static com.example.admin.registerwxdemo.Utils.CommandUtils.execCommand;
/**
* Created by admin on 2017/12/28.
*/
// 微信辅助类
public class WechatUtils {
private static final String TAG = "WechatUtils";
// 是否有微信
public static boolean hasWechatApp(Context context) {
PackageManager pkgManager = context.getPackageManager();
List<PackageInfo> packageInfoList = pkgManager.getInstalledPackages(0);
if (packageInfoList!=null){
for (int i = 0; i < packageInfoList.size(); i++) {
if ((packageInfoList.get(i).packageName).equals("com.tencent.mm")){
PackageInfo info = packageInfoList.get(i);
// wxVersion = info.versionName;
Log.i(TAG, "hasWechatApp: 已安装微信 ");
return true;
}
}
}
Log.i(TAG, "hasWechatApp: 微信未安装");
return false;
}
// 杀死微信
public static void killWechatByCommand(boolean isRoot) throws IOException {
// Process exec = Runtime.getRuntime().exec("adb shell am force-stop com.tencent.mm");
// Log.i(TAG, "killWechat: "+exec.toString());
Runtime.getRuntime().exec("su");
// 真机不能用adb shell的方式来执行命令。模拟器就可以
// execCMD("am force-stop com.tencent.mm");
// 上面这种就可以的,但是不够完善,采用这个比较完美,还有返回信息输出的。
execCommand("am force-stop com.tencent.mm",isRoot);
}
/**
* 通过 Intent的方式打开微信
*
* @param context 上下文
*/
public static void openWechat(Context context) {
Intent intent = new Intent();
ComponentName cmp=new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
context.startActivity(intent);
}
/**
* 通过命令行的方式打开微信
* @param isRoot 手机是否root
*/
public static void openWechatByCommand(boolean isRoot){
execCommand("am start -n com.tencent.mm/com.tencent.mm.ui.LauncherUI",isRoot);
}
/**
* 每次都打开一个新的wechat,也就是SingleTask模式
* @param isRoot
*/
public static void openNewWechatByCommand(boolean isRoot){
execCommand("am start --activity-clear-top com.tencent.mm/com.tencent.mm.ui.LauncherUI",isRoot);
}
/**
* 清理微信数据
* @param isRoot 手机是否root
* @throws IOException
*/
public static void clearWxData(boolean isRoot) throws IOException {
execCommand("pm clear com.tencent.mm",isRoot);
}
}