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权限或者和系统一样的签名。
前一种思路已经完全成功了,第二种思路验证了一部分。
先看第一种思路的办法: