清除Android中的数据并打开MainActivity.java文件

时间:2013-04-14 16:37:45

标签: android android-intent sharedpreferences

我正在开发一个应用程序,它接收用户的手机号码并将号码存储在本地文件中,并将其存储在云数据库中。我已经编写了应用程序逻辑,如果用户从应用程序退出,它将保持状态。当用户再次打开应用程序时,将使用相同的手机号码打开主页。我想清除Logout按钮上的所有数据,然后再次打开MobileHarvestMainActivity.java文件(要求再次从用户输入数字)。

我尝试了链接How to programmatically clear application data - 它有效,但在使用意图发送MainActivity页面时App崩溃了。

logout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);

                Toast.makeText(getBaseContext(), "clicked", Toast.LENGTH_SHORT).show();


            Intent intent = new Intent(ListeningAndSharingHomeActivity.this,MyActivity.class);
          //  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
            startActivity(intent);
            finish();



        }
    });

}



public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_my);


    clearApplicationData();


    }

    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 *******************");

    }

    }

    }
    calling();
    }

    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();


    }

void calling() {

Intent intent = new Intent(getApplicationContext(), MobileHarvestMainActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

}

}

0 个答案:

没有答案