尝试使用包安装程序安装 apk 时遇到问题。安装失败并显示非常模糊的状态消息“INSTALL_FAILED_INTERNAL_ERROR:会话已放弃”。
这是安装包的代码:
public static void installPackage(Context context, String packageName, String apkPath)
{
Logger.debug("Installing package " + apkPath);
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
try {
Logger.debug("Creating session");
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
Logger.debug("Copying buffer");
OutputStream out = session.openWrite(packageName, 0, -1);
FileInputStream fileInputStream = new FileInputStream(new File(apkPath));
byte[] buffer = new byte[1024];
int len = fileInputStream.read(buffer);
while(len != -1)
{
out.write(buffer,0, len);
len = fileInputStream.read(buffer);
}
session.fsync(out);
fileInputStream.close();
out.close();
Logger.debug("Buffer copied, sending intent");
Intent intent = new Intent(INSTALL_COMPLETE_ACTION);
IntentSender intentSender = PendingIntent.getBroadcast(context, sessionId,
intent, 0) .getIntentSender();
session.commit(intentSender);
session.close();
}
catch(IOException e)
{
Logger.error("IOException while installing package");
Logger.error(e);
}
catch(Exception e)
{
Logger.error("Unknown exception while installing package");
Logger.error(e);
}
}
这是处理意图的广播接收器:
private BroadcastReceiver mInstallReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Logger.debug("Received intent");
Logger.debug("Intent action: " + intent.getAction());
int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE);
Logger.debug("Intent status: " + status);
String message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
Logger.debug("Status message: " + message);
if(status == PackageInstaller.STATUS_PENDING_USER_ACTION)
{
Intent innerIntent = (Intent) intent.getParcelableExtra(Intent.EXTRA_INTENT);
startActivity(innerIntent);
}
}
};
这是广播接收器生成的日志:
11-12 15:22:08.364 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:08.365 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:08.365 7179 7179 D DummyActivity @ onReceive() Intent status: -1
11-12 15:22:08.365 7179 7179 D DummyActivity @ onReceive() Status message: null
11-12 15:22:08.378 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:08.378 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:08.379 7179 7179 D DummyActivity @ onReceive() Intent status: -1
11-12 15:22:08.379 7179 7179 D DummyActivity @ onReceive() Status message: null
11-12 15:22:08.387 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:08.387 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:08.387 7179 7179 D DummyActivity @ onReceive() Intent status: -1
11-12 15:22:08.388 7179 7179 D DummyActivity @ onReceive() Status message: null
11-12 15:22:09.155 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.155 7179 7179 D DummyActivity @ onReceive() Intent status: 1
11-12 15:22:09.155 7179 7179 D DummyActivity @ onReceive() Status message: INSTALL_FAILED_INTERNAL_ERROR: Session relinquished
11-12 15:22:09.155 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:09.155 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.156 7179 7179 D DummyActivity @ onReceive() Intent status: 1
11-12 15:22:09.156 7179 7179 D DummyActivity @ onReceive() Status message: INSTALL_FAILED_INTERNAL_ERROR: Session relinquished
11-12 15:22:09.156 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:09.157 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.157 7179 7179 D DummyActivity @ onReceive() Intent status: 1
11-12 15:22:09.157 7179 7179 D DummyActivity @ onReceive() Status message: INSTALL_FAILED_INTERNAL_ERROR: Session relinquished
11-12 15:22:09.214 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:09.214 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.215 7179 7179 D DummyActivity @ onReceive() Intent status: 4
11-12 15:22:09.215 7179 7179 D DummyActivity @ onReceive() Status message: INSTALL_PARSE_FAILED_NOT_APK: Failed parse during installPackageLI: Failed to parse /data/app/vmdl1253852664.tmp
11-12 15:22:09.215 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:09.215 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.215 7179 7179 D DummyActivity @ onReceive() Intent status: 4
11-12 15:22:09.216 7179 7179 D DummyActivity @ onReceive() Status message: INSTALL_PARSE_FAILED_NOT_APK: Failed parse during installPackageLI: Failed to parse /data/app/vmdl1253852664.tmp
11-12 15:22:09.216 7179 7179 D DummyActivity @ onReceive() Received intent
11-12 15:22:09.216 7179 7179 D DummyActivity @ onReceive() Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.216 7179 7179 D DummyActivity @ onReceive() Intent status: 4
11-12 15:22:09.216 7179 7179 D DummyActivity @ onReceive() Status message: INSTALL_PARSE_FAILED_NOT_APK: Failed parse during installPackageLI: Failed to parse /data/app/vmdl1253852664.tmp
提前致谢。