专业的编程技术博客社区

网站首页 > 博客文章 正文

Android自动静默安装(手机静默安装)

baijin 2024-09-01 13:04:46 博客文章 7 ℃ 0 评论
private boolean installUseRoot(String filePath) {
if (TextUtils.isEmpty(filePath))
throw new IllegalArgumentException("Please check apk file path!");
 boolean result = false;
Process process = null;
OutputStream outputStream = null;
BufferedReader errorStream = null;
 try {
process = Runtime.getRuntime().exec("su");
outputStream = process.getOutputStream();
String command = "pm install -r " + filePath + "\n";
outputStream.write(command.getBytes());
outputStream.flush();
outputStream.write("exit\n".getBytes());
outputStream.flush();
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder msg = new StringBuilder();
String line;
 while ((line = errorStream.readLine()) != null) {
msg.append(line);
}
Log.d(TAG, "install msg is " + msg);
 if (!msg.toString().contains("Failure")) {
result = true;
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (errorStream != null) {
errorStream.close();
}
} catch (IOException e) {
outputStream = null;
errorStream = null;
process.destroy();
}
}
return result;
}
private void installUseAS(String filePath) {
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileProvider", file);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
mContext.startActivity(intent);
 if (!isAccessibilitySettingsOn(mContext)) {
toAccessibilityService();
sendEmptyMessage(3);
}
}
private void toAccessibilityService() {
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
mContext.startActivity(intent);
}
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
 final String service = mContext.getPackageName() + "/" + InstallAccessibilityService.class.getCanonicalName();
 try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
 if (accessibilityEnabled == 1) {
Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
 if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
 while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
 if (accessibilityService.equalsIgnoreCase(service)) {
Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
 return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表