[英]How to install/update/remove APK using “PackageInstaller” class in Android L?
Plz檢查下面的classe並給我如何使用它們的建議https://developer.android.com/reference/android/content/pm/PackageInstaller.html https://developer.android.com/reference/android/content /pm/PackageInstaller.Session.html
所以請給我一個安裝/更新/刪除應用程序的示例。 新應用程序是否可能安裝在設備配置文件所有者中?
沒有Android M以上的系統權限就可以。
if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid)
== PackageManager.PERMISSION_GRANTED)
|| (installerUid == Process.ROOT_UID)
|| mIsInstallerDeviceOwner) {
mPermissionsAccepted = true;
} else {
mPermissionsAccepted = false;
}
設備所有者靜默安裝和卸載應用程序:
設備所有者現在可以使用PackageInstaller API以獨立於Google Play for Work的方式靜默安裝和卸載應用程序。
這可以在Android 6.0及更高版本中使用。
一旦您的應用獲得了設備所有者權限,我們就可以無需任何用戶干預即可靜默安裝,卸載和更新。
public static boolean installPackage(Context context, InputStream in, String packageName)
throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
// set params
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
OutputStream out = session.openWrite("COSU", 0, -1);
byte[] buffer = new byte[65536];
int c;
while ((c = in.read(buffer)) != -1) {
out.write(buffer, 0, c);
}
session.fsync(out);
in.close();
out.close();
session.commit(createIntentSender(context, sessionId));
return true;
}
private static IntentSender createIntentSender(Context context, int sessionId) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
new Intent(ACTION_INSTALL_COMPLETE),
0);
return pendingIntent.getIntentSender();
}
卸載:
String appPackage = "com.your.app.package";
Intent intent = new Intent(getActivity(), getActivity().getClass());
PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
PackageInstaller mPackageInstaller = getActivity().getPackageManager().getPackageInstaller();
mPackageInstaller.uninstall(appPackage, sender.getIntentSender());
您無法使用PackageInstaller.Session.commit()在新創建的用戶中靜默安裝第三方應用程序,而無需特定的“權限”。
你要么需要:
ROOT_UID
運行該過程。 這意味着您必須根設備。 來自Android源代碼 :
if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid) == PackageManager.PERMISSION_GRANTED)
|| (installerUid == Process.ROOT_UID)) {
mPermissionsAccepted = true;
} else {
mPermissionsAccepted = false;
}
如果您既沒有root訪問權限也沒有INSTALL_PACKAGES
權限,則會提示用戶詢問他是否確認了權限。 然后在PackageInstaller's
會話的提交process
中使用此確認。 顯然,在這種情況下,這不是透明的,因為用戶必須手動確認您的應用程序的安裝。
提供的安裝方法@amalBit對我不起作用。 這很奇怪,因為這是在Google Sample中實現的方式 。
這個答案幫助我找到了解決方案。 我不得不改變代碼的某些部分。 這是我的實現:
public static void installPackage(Context context, InputStream inputStream)
throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
int sessionId = packageInstaller.createSession(new PackageInstaller
.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
long sizeBytes = 0;
OutputStream out = null;
out = session.openWrite("my_app_session", 0, sizeBytes);
int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = inputStream.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
}
session.fsync(out);
inputStream.close();
out.close();
// fake intent
IntentSender statusReceiver = null;
Intent intent = new Intent(context, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
session.close();
}
可以像這樣調用此方法:
InputStream inputStream = getActivity().getAssets().open("my_awesome_app.apk");
InstallationHelper.installPackage(getActivity(), inputStream);
雖然我的設備所有者限制用戶安裝應用程序和未知來源,但這對我也有用。 即使我將此示例作為設備管理員運行,我也得到了java.lang.SecurityException: User restriction prevents installing.
openSession
正在檢查權限。 通過這種簡單的修改,可以僅在短方法調用期間重置用戶限制。
public static DevicePolicyManager getDpm(Context context) {
return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
}
public static ComponentName getAdmin(Context context) {
return new ComponentName(context, MyDevicePolicyReceiver.class);
}
public static void addMyRestrictions(Context context) {
getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS);
getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
}
public static void clearMyRestrictions(Context context) {
getDpm(context).clearUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS);
getDpm(context).clearUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
}
public static void installPackage(Context context, InputStream inputStream)
throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
int sessionId = packageInstaller.createSession(new PackageInstaller
.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));
//openSession checks for user restrictions
clearMyRestrictions(context);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
addMyRestrictions(context);
long sizeBytes = 0;
OutputStream out = null;
out = session.openWrite("my_app_session", 0, sizeBytes);
int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = inputStream.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
}
session.fsync(out);
inputStream.close();
out.close();
// fake intent
IntentSender statusReceiver = null;
Intent intent = new Intent(context, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
session.close();
}
請注意異常處理。
你只需清除你的限制
public static DevicePolicyManager getDpm(Context context) {
return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
}
public static ComponentName getAdmin(Context context) {
return new ComponentName(context, MyDevicePolicyReceiver.class);
}
public static void addMyRestrictions(Context context) {
getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS);
getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
}
public static void clearMyRestrictions(Context context) {
getDpm(context).clearUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS);
getDpm(context).clearUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
}
public static void installPackage(Context context, InputStream inputStream)
throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
int sessionId = packageInstaller.createSession(new PackageInstaller
.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));
//openSession checks for user restrictions
clearMyRestrictions(context);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
long sizeBytes = 0;
OutputStream out = null;
out = session.openWrite("my_app_session", 0, sizeBytes);
int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = inputStream.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
}
session.fsync(out);
inputStream.close();
out.close();
// fake intent
IntentSender statusReceiver = null;
Intent intent = new Intent(context, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
session.close();
}
在Android Api-21下面是代碼片段,我們可以通過它靜默安裝apk。
private void runInstallWrite() throws IOException, RemoteException {
long sizeBytes = -1;
String opt;
while ((opt = nextOption()) != null) {
if (opt.equals("-S")) {
sizeBytes = Long.parseLong(nextOptionData());
} else {
throw new IllegalArgumentException("Unknown option: " + opt);
}
}
final int sessionId = Integer.parseInt(nextArg());
final String splitName = nextArg();
String path = nextArg();
if ("-".equals(path)) {
path = null;
} else if (path != null) {
final File file = new File(path);
if (file.isFile()) {
sizeBytes = file.length();
}
}
final SessionInfo info = mInstaller.getSessionInfo(sessionId);
PackageInstaller.Session session = null;
InputStream in = null;
OutputStream out = null;
try {
session = new PackageInstaller.Session(mInstaller.openSession(sessionId));
if (path != null) {
in = new FileInputStream(path);
} else {
in = new SizedInputStream(System.in, sizeBytes);
}
out = session.openWrite(splitName, 0, sizeBytes);
int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = in.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
if (info.sizeBytes > 0) {
final float fraction = ((float) c / (float) info.sizeBytes);
session.addProgress(fraction);
}
}
session.fsync(out);
System.out.println("Success: streamed " + total + " bytes");
} finally {
IoUtils.closeQuietly(out);
IoUtils.closeQuietly(in);
IoUtils.closeQuietly(session);
}
}
上面的代碼是由框架了這里
我可以在LoLiipop中將此代碼與device_owner或普通用戶一起使用嗎?
答案 - 否由於在android框架中有@hide標簽的apis,雖然在API 21中引入了PackageManager.Session但我們不能使用新的PAckageManager.Session(),因為它在API 21中@hide。
如果您仍想通過framework.jar使用此代碼,則需要構建Lolippop源代碼並從/..../ framework.jar中提取jar並調用apis以上。
安裝:
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory() + "/download/" + APK_NAME)),
"application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(promptInstall);
卸載:
Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null));
startActivity(intent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.