android9 apk自动安装功能,Android自动安装APK

我有一个webview,基本上能够拦截各种链接,视频,apks,hrefs.

现在,我想要的是,一旦我从网址下载APK,它就会自动安装:

这是shouldOverrideUrlLoading()代码的一部分:

else if(url.endsWith(".apk"))

{

mWebView.setDownloadListener(new DownloadListener() {

public void onDownloadStart(final String url, String userAgent,

String contentDisposition, String mimetype,

long contentLength) {

}

});

Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));

startActivity(intent);

return true;

如果我加

intent.setDataAndType(Uri.parse(url), "application/vnd.android.package-archive");

比应用程序崩溃…

关于该怎么做的任何想法?

编辑:我能够自动启动下载和安装包(使用sleep()):

else if(url.endsWith(".apk"))

{

mWebView.setDownloadListener(new DownloadListener() {

public void onDownloadStart(final String url, String userAgent,

String contentDisposition, String mimetype,

long contentLength) {

}

});

Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));

startActivity(intent);

String fileName = Environment.getExternalStorageDirectory() + "/download/" + url.substring( url.lastIndexOf('/')+1, url.length() );

install(fileName);

return true;

并且,维生素建议:

protected void install(String fileName) {

Intent install = new Intent(Intent.ACTION_VIEW);

install.setDataAndType(Uri.fromFile(new File(fileName)),

"application/vnd.android.package-archive");

startActivity(install);

}

但是,我无法捕获下载完成的确切时间,可能需要创建我自己的下载功能而不使用浏览器的一个,任何想法?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 在切入后台后会暂停所有正在进行的操作,包括 APK安装过程。因此,如果用户在 APK 安装过程中切入后台,那么 APK 安装过程会被暂停。 如果你想让 APK 在切入后台后自动安装,可以考虑使用服务来安装 APK。具体来说,你可以在服务中使用 Intent 来启动系统的安装程序来安装 APK。这样,即使用户切入后台,服务仍然在后台运行,安装程序也会在后台自动安装 APK。 下面是一个示例代码,用于在服务中安装 APK: ``` public class ApkInstallService extends Service { private String mApkFilePath; @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { mApkFilePath = intent.getStringExtra("apk_file_path"); if (mApkFilePath != null) { installApk(); } } return super.onStartCommand(intent, flags, startId); } private void installApk() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(mApkFilePath)), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 在你的 Activity 中,你可以通过以下代码来启动服务: ``` Intent intent = new Intent(this, ApkInstallService.class); intent.putExtra("apk_file_path", "your_apk_file_path"); startService(intent); ``` 这样,即使用户在 APK 安装过程中切入后台,服务仍然会在后台运行,安装程序也会在后台自动安装 APK

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值