我的目标是安装一个存储在设备上的.apk文件。我试过使用ACTION_VIEW意图,但我认为它在安卓10中已经被废弃了。
我已经浏览了一半的网页,但大多数答案要么是使用废弃的意图,kotlin,要么就是不起作用。
这是我的密码
private void installApk() throws IOException {
if (!requireContext().getPackageManager().canRequestPackageInstalls()) {
startActivity(new Intent(
Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
Uri.parse("package:" + requireContext().getPackageName())));
} else {
// Log.d(TAG, "File Exists?: " + outputFile.exists() + " ; " + outputFile.getAbsolutePath() + " ; " + "can read?" + outputFile.canRead() + " ; " + "can write?" + outputFile.canWrite());
PackageInstaller packageInstaller = requireContext().getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller
.SessionParams.MODE_FULL_INSTALL);
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
addApkToInstallSession(session);
Intent callbackIntent = new Intent(requireContext(), APKInstallService.class);
PendingIntent pendingIntent = PendingIntent.getService(requireContext(), 0, callbackIntent, 0);
// This is sending the result of the installation to the APKInstallService.
IntentSender statusReceiver = pendingIntent.getIntentSender();
session.commit(statusReceiver);
}
}这是我在onClickListener中调用的方法
private void addApkToInstallSession(PackageInstaller.Session session)
throws IOException {
try (OutputStream packageInSession = session.openWrite("package", 0, -1);
InputStream is = requireContext().getContentResolver().openInputStream(fileURI)) {
byte[] buffer = new byte[16384];
int n;
while ((n = is.read(buffer)) >= 0) {
packageInSession.write(buffer, 0, n);
}
}
}将文件添加到会话中。
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class APKInstallService extends Service {
private static final String TAG = "APKInstallService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, -999);
switch (status) {
case PackageInstaller.STATUS_PENDING_USER_ACTION:
Log.d(TAG, "Requesting user confirmation for installation");
// This is a way to get the intent that was passed to the service.
//Intent confirmationIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);
//confirmationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//try {
// startActivity(confirmationIntent);
//} catch (Exception e) {
//No action
// }
break;
case PackageInstaller.STATUS_SUCCESS:
Log.d(TAG, "Installation succeed");
break;
default:
Log.d(TAG, "Installation failed");
break;
}
stopSelf();
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}得到结果
我对编码很陌生,如果这是个愚蠢的问题,我很抱歉
发布于 2022-03-16 15:46:55
看起来问题在于,我的模拟器没有安装gapps,尽管在任何地方都没有声明,但显然是这种操作的依赖。虽然我会推荐CommonsWare在他的评论中提到的章节,因为它们比稀少的文档要全面得多。
https://stackoverflow.com/questions/71455373
复制相似问题