Android 6.0 - 卸载应用时删除外部存储文件

Mat*_*nti 13 android download-manager android-external-storage android-6.0-marshmallow

我的应用程序使用DownloadManager将文件下载到设备的Music文件夹的子目录中.

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
...
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/MyStuff/song.mp3");
request.setDestinationUri(Uri.fromFile(file));
Run Code Online (Sandbox Code Playgroud)

我注意到当从运行Marshmallow的设备上卸载应用程序时,文件正在被删除(这在旧的OS版本中不会发生).你有什么想法吗?

谢谢

tyn*_*ynn 5

这是由名为DownloadReceiver的内部类完成的,并在包清单中定义com.android.providers.downloads

<receiver android:name=".DownloadReceiver" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="android.intent.action.UID_REMOVED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

这个android.intent.action.UID_REMOVED动作引人注目.它是在Lollipop中引入触发handleUidRemoved()执行调用的

resolver.delete(ALL_DOWNLOADS_CONTENT_URI, Constants.UID + "=" + uid, null);
Run Code Online (Sandbox Code Playgroud)

  • @FaultException它没有连接到真实用户.它指的是分配给应用程序的Linux系统用户标识.https://developer.android.com/guide/components/fundamentals.html (2认同)