update:2020.05.19

fix:修复设备重置,静默删除
add:
This commit is contained in:
2020-05-19 18:24:33 +08:00
parent fdbecb973e
commit a9dbcb17a6
14 changed files with 307 additions and 76 deletions

View File

@@ -39,6 +39,9 @@ import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.crypto.interfaces.PBEKey;
import cn.jpush.android.api.PushNotificationBuilder;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
@@ -113,7 +116,7 @@ public class ApkUtils {
/**
* 卸载一个app
*/
public static void uninstall(Context context, String packageName) {
public static void unInstall(Context context, String packageName) {
//通过程序的包名创建URI
Uri packageURI = Uri.parse("package:" + packageName);
//创建Intent意图
@@ -273,12 +276,12 @@ public class ApkUtils {
public void onNext(Integer value) {
if (value == 2) {
//安装成功
Utils.showToast(context, "安装成功");
ToastUtil.show("安装成功");
Log.e("mjsheng", "-----------安装成功-----------");
} else {
//安装错误
Log.e("mjsheng", "------------安装错误-----------");
Utils.showToast(context, "安装失败");
ToastUtil.show("安装失败");
}
}
@@ -438,7 +441,7 @@ public class ApkUtils {
}
} catch (IOException e) {
e.printStackTrace();
Log.e("fht", e.getMessage());
Log.e("fht", "copyApkFile" + e.getMessage());
}
return success;
}
@@ -498,6 +501,59 @@ public class ApkUtils {
}
}
/**
* 根据包名卸载应用
*
* @param packageName
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void uninstall(Context context, String packageName) {
Intent broadcastIntent = new Intent(context, InstallResultReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
packageInstaller.uninstall(packageName, pendingIntent.getIntentSender());
}
public static boolean pmUninstall(String packageName) {
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
try {
process = new ProcessBuilder("pm", "uninstall", packageName).start();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
} catch (Exception e) {
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (Exception e) {
}
if (process != null) {
process.destroy();
}
}
//如果含有“success”单词则认为卸载成功
return successMsg.toString().equalsIgnoreCase("success");
}
private static Class<?>[] getParamTypes(Class<?> cls, String mName) {
Class<?> cs[] = null;
Method[] mtd = cls.getMethods();

View File

@@ -3,21 +3,37 @@ package com.mjsheng.myappstore.utils;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.content.pm.PackageInstaller;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import java.util.Iterator;
import java.util.Set;
import static android.content.pm.PackageInstaller.STATUS_PENDING_USER_ACTION;
public class InstallResultReceiver extends BroadcastReceiver {
private static final String TAG = "InstallResultReceiver";
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
if (intent != null) {
final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS,
PackageInstaller.STATUS_FAILURE);
if (status == PackageInstaller.STATUS_SUCCESS) {
// success
String PACKAGE_NAME = intent.getStringExtra("android.content.pm.extra.PACKAGE_NAME");
Log.e(TAG, "APP Install Success!");
} else {
String msg = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
}
}
// String s = intent.getAction();
// Log.e("fht", s);
// Bundle extras = intent.getExtras();
@@ -26,11 +42,11 @@ public class InstallResultReceiver extends BroadcastReceiver {
// while (iterator.hasNext()) {
// Log.d("KEY", iterator.next());
// }
String STATUS = intent.getStringExtra("android.content.pm.extra.STATUS");
String PACKAGE_NAME = intent.getStringExtra("android.content.pm.extra.PACKAGE_NAME");
String SESSION_ID = intent.getStringExtra("android.content.pm.extra.SESSION_ID");
String STATUS = intent.getStringExtra(PackageInstaller.EXTRA_STATUS);
String PACKAGE_NAME = intent.getStringExtra(PackageInstaller.EXTRA_PACKAGE_NAME);
String SESSION_ID = intent.getStringExtra(PackageInstaller.EXTRA_SESSION_ID);
String STATUS_MESSAGE = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
String LEGACY_STATUS = intent.getStringExtra("android.content.pm.extra.LEGACY_STATUS");
String STATUS_MESSAGE = intent.getStringExtra("android.content.pm.extra.STATUS_MESSAGE");
// Log.e("fht", STATUS);
// Log.e("fht", PACKAGE_NAME);
// Log.e("fht", SESSION_ID);

View File

@@ -5,7 +5,9 @@ import android.app.admin.DevicePolicyManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
@@ -24,6 +26,7 @@ import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
@@ -43,6 +46,7 @@ import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.MyApplication;
import com.mjsheng.myappstore.comm.CommonDatas;
import java.io.FileReader;
import java.io.InputStreamReader;
@@ -883,5 +887,31 @@ public class Utils {
}
}
synchronized public static void doMasterClear(Context context) {
if (getBatteryLevel(context) >= CommonDatas.MIN_POWER) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
Intent intent = new Intent("android.intent.action.FACTORY_RESET");
intent.setPackage("android");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.putExtra("android.intent.extra.REASON", "MasterClearConfirm");
intent.putExtra("android.intent.extra.WIPE_EXTERNAL_STORAGE", false);
intent.putExtra("com.android.internal.intent.extra.WIPE_ESIMS", false);
context.sendBroadcast(intent);
} else {
Intent intent = new Intent("android.intent.action.MASTER_CLEAR");
// intent.setPackage("com.android.settings");
context.sendBroadcast(intent);
}
} else {
MySQLData.SetBooleanData(context, CommonDatas.IS_RESET, true);
}
}
synchronized private static int getBatteryLevel(Context mContext) {
if (Build.VERSION.SDK_INT >= 21)
return ((BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE)).getIntProperty(4);
Intent intent = (new ContextWrapper(mContext)).registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
return intent.getIntExtra("level", -1) * 100 / intent.getIntExtra("scale", -1);
}
}