请检查下面的类并给我有关如何使用它们的建议 https://developer.android.com/reference/android/content/pm/PackageInstaller.html https://developer.android.com/reference/android/content /pm/PackageInstaller.Session.html
所以请给我一个安装/更新/删除应用程序的例子。新应用程序是否有可能安装在设备配置文件所有者中?
请检查下面的类并给我有关如何使用它们的建议 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
权限,则会提示用户一条消息,询问他是否确认权限。process
然后在会话提交期间使用此确认PackageInstaller's
。显然,在这种情况下,这是不透明的,因为用户必须手动确认您的应用程序的安装。
@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);
您只需清除您的限制
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();
}
这也适用于我,尽管我的设备所有者限制用户安装应用程序和未知来源。即使我以设备管理员身份运行此示例,我也有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();
}
请注意异常处理。
在下面的 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);
}
}
上面的代码取自Framework here
我可以将此代码与 LoLiipop 中的 device_owner 或普通用户一起使用吗?
答案 - 否 由于在 android 框架中有被 @hide 标签的 api,虽然 PackageManager.Session 是在 API 21 中引入的,但我们不能使用 new PAckageManager.Session(),因为它在 API 21 中是 @hide。
如果您还想通过 framework.jar 使用此代码,您需要构建 Lolippop 源代码并从 out/..../framework.jar 中提取 jar 并调用上面的 api。
安装:
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);