procedure _InstallApk(Apk: string span>);
var
LFile: JFile;
LIntent: JIntent;
begin
LFile := TJFile.JavaClass.init(StringToJString(ExtractFilePath(Apk)), StringToJstring(ExtractFileName(Apk)));
LIntent := TJIntent.Create;
LIntent.setAction(TJIntent.JavaClass.ACTION_VIEW);
LIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
LIntent.setDataAndType(TJnet_Uri.JavaClass.fromFile(LFile), StringToJString('application/vnd.android.package-archive'));
TAndroidHelper.Context.startActivity(LIntent);
end;
procedure InstallApk(Apk: string);
var
LFile: JFile;
LIntent: JIntent;
LNet_Uri: Jnet_Uri;
begin
if not TOSVersion.Check(7, 0) then
begin
_InstallApk(Apk);
Exit;
end;
LFile := TJFile.JavaClass.init(StringToJString(ExtractFilePath(Apk)), StringToJstring(ExtractFileName(Apk)));
LIntent := TJIntent.Create;
if TOSVersion.Check(8, 0) then
LIntent.setAction(TJIntent.JavaClass.ACTION_INSTALL_PACKAGE)
else
LIntent.setAction(TJIntent.JavaClass.ACTION_VIEW);
LIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
if TOSVersion.Check(7, 0) then
begin
// fix: android.os.FileUriExposedException: exposed beyond app through Intent.getData( )
// Project -> Options -> Application -> Entitlement List -> Secure File Sharing -> Check it
LIntent.addFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
LNet_Uri := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
StringToJString(JStringToString(TAndroidHelper.Context.getPackageName) + '.fileprovider '), LFile);
end
else
LNet_Uri := TJnet_Uri.JavaClass.fromFile(LFile);
LIntent.setDataAndType(LNet_Uri, StringToJString('application/vnd.android .package-archive'));
TAndroidHelper.Context.startActivity(LIntent);
end;
For Android 8.0 and above, add permissions in the manifest
Thanks to the author of codecoolie, FFmpeg for Delphi http://www.CCAVC.com http://www.DelphiFFmpeg.com, yes Opportunity to support it!
Original address: http://bbs.2ccc.com/topic.asp?topicid=568834
procedure _InstallApk(Apk: string);
var
LFile: JFile;
LIntent: JIntent;
begin
LFile := TJFile.JavaClass.init(StringToJString(ExtractFilePath(Apk)), StringToJstring(ExtractFileName(Apk)));
LIntent := TJIntent.Create;
LIntent.setAction(TJIntent.JavaClass.ACTION_VIEW);
LIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
LIntent.setDataAndType(TJnet_Uri.JavaClass.fromFile(LFile), StringToJString('application/vnd.android.package-archive'));
TAndroidHelper.Context.startActivity(LIntent);
end;
procedure InstallApk(Apk: string);
var
LFile: JFile;
LIntent: JIntent;
LNet_Uri: Jnet_Uri;
begin
if not TOSVersion.Check(7, 0) then
begin
_InstallApk(Apk);
Exit;
end;
LFile := TJFile.JavaClass.init(StringToJString(ExtractFilePath(Apk)), StringToJstring(ExtractFileName(Apk)));
LIntent := TJIntent.Create;
if TOSVersion.Check(8, 0) then
LIntent.setAction(TJIntent.JavaClass.ACTION_INSTALL_PACKAGE)
else
LIntent.setAction(TJIntent.JavaClass.ACTION_VIEW);
LIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
if TOSVersion.Check(7, 0) then
begin
// fix: android.os.FileUriExposedException: exposed beyond app through Intent.getData( )
// Project -> Options -> Application -> Entitlement List -> Secure File Sharing -> Check it
LIntent.addFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
LNet_Uri := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
StringToJString(JStringToString(TAndroidHelper.Context.getPackageName) + '.fileprovider '), LFile);
end
else
LNet_Uri := TJnet_Uri.JavaClass.fromFile(LFile);
LIntent.setDataAndType(LNet_Uri, StringToJString('application/vnd.android .package-archive'));
TAndroidHelper.Context.startActivity(LIntent);
end;