清除应用程序的数据编程方式 [英] Clear Application's Data Programatically

查看:135
本文介绍了清除应用程序的数据编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式清除我的应用程序的数据。

I want to clear my application's data programatically.

应用程序的数据可能包含如数据库什么,共享preferences,内部,外部文件或应用程序中创建的任何其他文件。

Application's data may contain anything like databases, shared preferences, Internal-External files or any other files created within the application.

我知道我们可以在移动设备通过清除数据:

I know we can clear data in the mobile device through:

设置 - >应用程序 - > ManageApplications-> My_application->清除   数据

Settings->Applications-> ManageApplications-> My_application->Clear Data

不过,我需要通过一个Android程序做上述的事情吗?

But I need to do the above thing through an Android Program?

在此先感谢...

推荐答案

我只是把教程从<一个href="http://www.hrupin.com/2011/11/how-to-clear-user-data-in-your-android-application-programmatically">the链接ihrupin张贴这里在这个岗位。

I'm just putting the tutorial from the link ihrupin posted here in this post.

package com.hrupin.cleaner;

import java.io.File;

import android.app.Application;
import android.util.Log;

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib")) {
                    deleteDir(new File(appDir, s));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }
}

所以,如果你想有一个按钮,要做到这一点,你需要调用MyApplication.getInstance()。 clearApplicationData()从内onClickListener

So if you want a button to do this you need to call MyApplication.getInstance(). clearApplicationData() from within an onClickListener

更新: 您的共享preferences实例可以守住你的数据并重新创建preferences后删除文件。所以,你会希望得到您的共享preference对象以及

Update: Your sharedPreferences instance might hold onto your data and recreate the preferences file after you delete it. So your going to want to get your SharedPreference object and

prefs.edit().clear().commit();

更新:

您需要添加安卓名=your.package.MyApplication应用程序标记里面AndroidManifest的.xml如果你没有这样做。否则,MyApplication.getInstance()返回null,产生的 NullPointerException异常

You need to add android:name="your.package.MyApplication" to the application tag inside AndroidManifest.xml if you had not done so. Else, MyApplication.getInstance() returns null, resulting a NullPointerException.

这篇关于清除应用程序的数据编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆