简体   繁体   中英

How to clear db when app is uninstalled in android

我使用数据库来存储消息,如果我卸载我的应用程序并再次重新安装相同的应用程序,数据库仍然相同,但我想清除我的数据库,如何解决这个问题?

To listen the uninstall event you have to implement a broadcast received, like:

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
    android:name=".UninstallIntentActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.DELETE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="package"  />
    </intent-filter>



</activity>

</application>

And then in your UninstallIntentActivity you have to use the next line to delete your database:

context.deleteDatabase(DATABASE_NAME);

You can see more about this topic here Listen Broadcast Before application uninstall

I hope this works for you.

below code can help you to delete your database from your application

/**
     * Re crate database
     * Delete all tables and create them again
     * */
    public void resetTables(){
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete(TABLE_NAME, null, null);
        db.close();
    }

make one method in class where you handle your database handling means. where you crate other methods like add,update etc..

存储在内部存储中的所有数据(包括数据库,首选项,本地数据文件)都将在卸载时删除。它们仅在应用程序更新时保持不变。

I think it's not ordinary situation, cause when you uninstall app - all db are removed automaticaly. But if it doesn't happen - try to clear data in Menu - Manage App - your app - Clear data before uninstalling.

在我的情况下,我只是忘了在DatabaseHelper类的onUpgrade方法中添加以下行

onCreate(sqLiteDatabase);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM