android 应用文件管理器,Android app 应用内更新弹出文件管理器,以及android7.0与8.0应用内更新差距所在...

应用内更新这个需求,可能大家都做过,也遇到过不少坑,比如我遇到的一个坑:

弹出文件管理器选择框

对于这种情况,虽然文件的类型已经设置为apk,

"application/vnd.android.package-archive"

但是,为什么会弹出文件管理器选择框呢,有点莫名其妙,后来我在Intent意图中加了一个Action,就不再提示文件管理器了

intent.setAction(Intent.ACTION_VIEW);

这个有点奇怪,查看一下Action_View的源码

/**

* Activity Action: Display the data to the user. This is the most common

* action performed on data -- it is the generic action you can use on

* a piece of data to get the most reasonable thing to occur. For example,

* when used on a contacts entry it will view the entry; when used on a

* mailto: URI it will bring up a compose window filled with the information

* supplied by the URI; when used with a tel: URI it will invoke the

* dialer.

*

Input: {@link #getData} is URI from which to retrieve data.

*

Output: nothing.

*/

@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)

public static final String ACTION_VIEW = "android.intent.action.VIEW";

这里我们可以看到,这个作用只是向用户显示数据。这是对数据执行的最常见的操作——它是您可以对一段数据使用的通用操作,以实现最合理的操作。关键在后面这个,例如,在“联系人”条目上使用时,它将查看该条目;在mailto:uri上使用时,它将打开一个包含由uri提供的信息的撰写窗口;与tel:uri一起使用时,它将调用拨号程序。

这个时候意思就很明显了,就是根据URI的类型,匹配对应的安装程序,那之前的DataType呢?

我个人的理解是,两个需要结合在一起用的原因是,有的设备对Android framework代码进行了修改,需要进行二次检验,毕竟,我在原生的android系统,和市面上的华为,小米等手机上测试,没有出现这个情况。只有我手里的一款ZEBRA品牌的手持终端出现了这个现象。

还有一个坑,为什么有时候调用startActivity之后,会闪退,这个就得看你的Authorities属性,匹配的对不对了,如果不对,肯定是会报错的。这个是唯一标识,希望大家引起注意。

最后,贴一下核心代码,仅供大家参考

File apkFile = new File(mSavePath, mHashMap.get("name"));

if (apkFile.exists()) {

Uri contentUri;

Intent intent = new Intent();

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

contentUri = FileProvider.getUriForFile(mContext, INSTALL_AUTHORITIES, apkFile);

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

} else {

contentUri = Uri.fromFile(apkFile);

}

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

PackageManager pm = mContext.getPackageManager();

//隐式意图校验

if (pm.queryIntentActivities(intent, 0).size() > 0 || pm.resolveActivity(intent, 0) != null

|| intent.resolveActivity(pm) != null) {

mContext.startActivity(intent);

}

}

从代码可以看到,做了一下7.0的校验,那么8.0呢,在AndroidManifest.xml文件中。加入这个就好了

讲到这里,讲解就完毕了,只是个人的一些见解。

还有一个问题抛出来,还请大家指教一下,就是我在安装完成之后,有一款机器并没有弹出安装完成的选项,正常情况,会弹出安装完成选项,可以选择完成和打开。我这直接就安装完成闪退了,但是应用确实是安装完成。

这个问题暂时没有得到解决,我接下来也会去查看源码,继续解决,对于一个偶现的现象,还得具体问题具体分析。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值