如何调用系统的包安装自己的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也就是如何setActionaddCategorysetDataAndType。而这些东西可以查看源代码获取到。那么这里就有一个很关键的问题了,怎么通过源代码找到需要的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>

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

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

posted @ 2014-03-25 12:51  Jacky_Chen-Fight  阅读(1817)  评论(0编辑  收藏  举报