读懂Android (1):使用Android内部的DownloadProvider下载文件,并获取cache权限

Android内部提供了一个DownloadProvider,是一个非常完整的下载工具,提供了很好的外部接口可以被其他应用程序调用,来完成下载工作。同时也提供和很好的下载、通知、存储等机制。
在Android的Browser等工具里面都用到了这个DownloadProvider。

但是很遗憾的是,这个DownloadProvider不对app开发人员开放,只作为内部使用。

我们现在去探究如何将DownloadProvider拿来给自己用。

让我们先找到DownloadProvider不能用的原因:
先找到它的源代码,在这个位置:/packages/providers/DownloadProvider
打开AndroidManifest.xml文件,里面有几个自定义的权限

    <!-- Allows access to the Download Manager -->
    <permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"
        android:label="@string/permlab_downloadManager"
        android:description="@string/permdesc_downloadManager"
        android:protectionLevel="signatureOrSystem" />

    <!-- Allows advanced access to the Download Manager -->
    <permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED"
        android:label="@string/permlab_downloadManagerAdvanced"
        android:description="@string/permdesc_downloadManagerAdvanced"
        android:protectionLevel="signatureOrSystem" />

    <!-- Allows filesystem access to /cache -->
    <permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM"
        android:label="@string/permlab_cacheFilesystem"
        android:description="@string/permdesc_cacheFilesystem"
        android:protectionLevel="signature" />

    <!-- Allows to send download completed intents -->
    <permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS"
        android:label="@string/permlab_downloadCompletedIntent"
        android:description="@string/permdesc_downloadCompletedIntent"
        android:protectionLevel="signature" />

这几个权限里面都是android:protectionLevel="signatureOrSystem" 或者   android:protectionLevel="signature", 这个意思是只有你的app拥有system权限,或者和系统一样的签名,才能调用它。

这里是问题的关键。那我们有两种思路:
一种思路是:将这个protectionLevel改成normal,重新编译DownloadProvider工程,让其他app可以直接调用。
另一种思路是:将你自己的app弄成system权限或者和系统一样的签名。

前一种思路已经完全成功了,第二种思路验证了一部分。

先看第一种思路的办法:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值