卸载应用程序后如何删除数据? [英] How to Delete the data when the App is Uninstalled?
本文介绍了卸载应用程序后如何删除数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个Android应用程序,该应用程序在SD卡中创建一个文件夹并存储一些图像.我要在卸载应用程序时删除该文件夹.请引导我.
I am developing an Android app which creates a folder in SD Card and stores some images. I want to remove that folder when the app is Uninstalled. Please guide me.
推荐答案
//,为此,您需要运行BroadcasrReciver并将接收器包含在androidmanifest.xml文件中
//for that you need to run the BroadcasrReciver and include the receiver in your androidmanifest.xml file
<receiver android:name="com.android.mobileasap.PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
//添加权限
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
//在那个PackageChangeReceiver中只是删除文件,我在下面的这段代码中删除了doc文件
// in that PackageChangeReceiver just delete the file I am deleting the doc file in this below code
public class PackageChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//this.context=context;
Uri data = intent.getData();
Log.d("hi", "Action: " + intent.getAction());
Log.d("hi", "The DATA: " + data);
String action=intent.getAction();
if(Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(action))
{
String PATH = Environment.getExternalStorageDirectory() + "/mycontent_download/";
File file = new File(PATH);
if (file.exists())
{
String listOfFiles [] = file.list();
if (listOfFiles!=null)
{
if (listOfFiles.length>0)
{
int size = listOfFiles.length;
for (int i=0; i<size; i++)
{
if (listOfFiles[i].substring(listOfFiles[i].length()-4, listOfFiles[i].length()).equalsIgnoreCase(".doc"))
{
File f1 = new File(PATH+listOfFiles[i]);
f1.delete();
}
}
}
}
}
这篇关于卸载应用程序后如何删除数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文