简体   繁体   中英

Trigger event when app is installed/uninstalled Android

I would like to get some information (date,etc) when an app is installed/uninstalled, I have found some useful posts about how to achieve this with ACTION_PACKAGE_REMOVED & ACTION_PACKAGE_ADDED intents,but even with this I can not get the correct approach ... any help will be appreciated. Thanks. This is the a simple code i wr

   <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter android:priority="100" >
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <data android:scheme="package" />
        </intent-filter>

public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    Log.d("Receiver", "Intent: " + intent.getAction());
}

}

You should remove

<data android:scheme="package" />

from your <intent-filter>

EDIT

The question is ... how I can trigger the install/uninstall event for my own application?

You will not get information related to your own package.

This is a workaround you can use to track updates for your own app.

  1. Implement a dummy database and override SQLiteOpenHelper.html onUpgrade() .The onUpgrade() method is called whenever a new database version is installed.For more info- refer here
  2. Now everytime you have a new update for an app..update the database version too.In this way you will have one to one mapping between database version and the app version.
  3. Now when the user updates the app onUpgrade() is called and you can write whatever logic you want to track the update.(If you are using database in your app, you might need some extra logic to differentiate between actual database upgrade and app upgrade.)

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