private void clearUserApps(){
PackageManager pm = context.getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
for (PackageInfo packageInfo : packages) {
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0){
Log.d("LYT","user app's pakcage name:" + packageInfo.packageName);
uninstallApp(packageInfo.packageName);
}
}
}
private void uninstallApp(String packageName){
Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE,Uri.parse("package:" + packageName));
uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, true);
uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
uninstallIntent.putExtra(Intent.EXTRA_KEY_CONFIRM, true);
((MainActivity)context).startActivityForResult(uninstallIntent,1);
}
public static String execCommand(String... command) {
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
String result = "";
try {
process = new ProcessBuilder().command(command).start();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1) {
baos.write(read);
}
inIs = process.getInputStream();
while ((read = inIs.read()) != -1) {
baos.write(read);
}
result = new String(baos.toByteArray());
if (inIs != null)
inIs.close();
if (errIs != null)
errIs.close();
process.destroy();
} catch (IOException e) {
result = e.getMessage();
}
return result;
}