如何调用系统的包安装自己的apk呢?

其实看着觉着好像不知道如何实现其实就是调用系统的intent来完成安装

案例驱动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void onSuccess(File t) {
      // 下载成功的条件下,替换应用程序
      // TODO 这里的东西不是很理解
      // 下载成功 替换安装 应用程序.
      // <action
      // android:name="android.intent.action.VIEW"
      // />
      // <category
      // android:name="android.intent.category.DEFAULT"
      // />
      // <data android:scheme="content" />
      // <data android:scheme="file" />
      // <data
      // android:mimeType="application/vnd.android.package-archive"
      // />
      Intent intent =  new Intent();
      intent.setAction( "android.intent.action.VIEW" );
      intent.addCategory( "android.intent.category.DEFAULT" );
      intent.setDataAndType(Uri.fromFile(t),
             "application/vnd.android.package-archive" );
      Log.i(TAG,  "成功进行下载" );
      startActivity(intent);
      super .onSuccess(t);
  }

这段代码是在断点下载功能中实现的,也就是在成功下载了应用的情况下,安装更新文件主要是需要掌握如何去调用这个Intent也就是如何setAction和addCategory和setDataAndType。而这些东西可以查看源代码获取到。那么这里就有一个很关键的问题了,怎么通过源代码找到需要的intent设置信息呢?

通过查找源代码中的apps中,发现有一个packageInstaller,看名字便知道这是一个安装应用的系统应用,那么如何调用它呢?直接查看他的清单文件可以看到如下信息:

案例驱动:

1
2
3
4
5
6
7
8
9
10
11
<activity android:name= ".PackageInstallerActivity"
                  android:configChanges= "orientation|keyboardHidden"
                  android:theme= "@style/TallTitleBarTheme" >
              <intent-filter>
                  <action android:name= "android.intent.action.VIEW" />
                  <category android:name= "android.intent.category.DEFAULT" />
                  <data android:scheme= "content" />
                  <data android:scheme= "file" />
                  <data android:mimeType= "application/vnd.android.package-archive" />
              </intent-filter>
          </activity>

那么就可以知道具体的调用方法了,将相应的参数设置就可以安装自己的应用了。

欢迎大家留言,这个是我曾经的一些笔记,现在想在网上记录下来,曾经的点点滴滴,当然肯定会有一些纰漏,还望指出

转载于:https://www.cnblogs.com/Jacky-Fight/p/3622888.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值