Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear db when app is uninstalled in android

Im using database to store messages, and if i uninstall my app and again reinstall the same app, the db remains same, but i want to clear my database, how to solve this?

like image 674
Vishnu Avatar asked Oct 23 '22 04:10

Vishnu


1 Answers

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.

like image 55
icastell Avatar answered Nov 01 '22 08:11

icastell