Plz检查下面的classe&给我一个如何使用它们的建议 https://developer.android.com/reference/android/content/pm/PackageInstaller.html https://developer.android.com/reference/android/content/pm/PackageInstaller.Session.html
请举个例子来安装/更新/删除应用。 新应用程序是否可能安装在设备配置文件所有者中?
答案 0 :(得分:22)
没有Android M onwards的系统权限。
<py4j.java_gateway.JavaPackage object at 0x7f07cc3f77f0>
设备所有者无人值守安装和卸载应用
设备所有者现在可以使用PackageInstaller API以独立于Google Play for Work的方式静默安装和卸载应用程序。
这可以在Android 6.0及更高版本中使用。
一旦您的应用获得设备所有者权限,我们就可以无需任何用户干预即可安装,卸载和更新。
if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid)
== PackageManager.PERMISSION_GRANTED)
|| (installerUid == Process.ROOT_UID)
|| mIsInstallerDeviceOwner) {
mPermissionsAccepted = true;
} else {
mPermissionsAccepted = false;
}
卸载:
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();
}
答案 1 :(得分:6)
您无法使用PackageInstaller.Session.commit()在没有特定“权限”的情况下在新创建的用户中静默安装第三方应用程序。
你需要:
ROOT_UID
身份运行该流程。这意味着您必须根设备。 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
期间使用此确认。显然,在这种情况下,这不是透明的,因为用户必须手动确认您的应用程序的安装。
答案 2 :(得分:4)
提供的安装方法@amalBit对我不起作用。这很奇怪,因为它是Google Sample中的实现方式。
这answer帮我找到了解决方案。我不得不改变代码的某些部分。这是我的实施:
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);
答案 3 :(得分:1)
这也适用于我,虽然我的设备所有者限制用户安装应用和未知来源。即使我将此示例作为设备管理员运行,我也获得了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();
}
请注意异常处理。
答案 4 :(得分:1)
您只需清除限制
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();
}
答案 5 :(得分:0)
安装:
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);
答案 6 :(得分:0)
在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);
}
}
以上代码来自框架here
我可以在LoLiipop中将此代码与device_owner或普通用户一起使用吗?
答案 - 没有 由于apis在android框架中是@hide标签,虽然在API 21中引入了PackageManager.Session,但我们不能使用新的PAckageManager.Session(),因为它在API 21中@hide。
如果您仍想通过framework.jar使用此代码,则需要构建Lolippop源代码并从/..../ framework.jar中提取jar并调用apis以上。