When installed from Google Play, you will receive a com.android.vending.INSTALL_REFERRER
broadcast during the install process. Simply create a broadcast receiver for this, and have a boolean value in your SharedPreferences
along the lines of fromGooglePlay
, which you can set to true in the receiver.
However, I do not think you can have similar receivers for other app stores, like Amazon's.
EDIT 1:
As @zapl pointed out in the comments, this will not work on Android 3.0 and above, as apps that have not been launched at least once are not eligible to receive broadcasts etc.
However, you can try an alternate solution:
In you app, have a constant boolean somewhere, something like GOOGLE_PLAY_BUILD
. When building an apk for Google Play, set this to true
, and when building an apk for manual distribution, set this to false
. Then simply check it's value at runtime, and act accordingly.
EDIT 2:
To address your concerns about rooted devices distributing your apps, here is a more elaborate system.
Each device has a unique ANDROID_ID (though apparently multi users on 4.2 have multiple ones, despite what the documentation says). Essentially you just need a unique device ID, so even a MAC address will do.
Have a table on a webserver to store the unique ID, whether or not you received the Google Play broadcast and the value of the Google Play flag. When the app is launched for the first time, add these details for that device to the table.
When the app is launched thereafter, simply check if all three of the details match a copy stored locally. If a user gets the app from a rooted user and installs the it, the Google Play flag will seem to match, however the install referrer part won't. Sadly, this will only work on pre honeycomb devices.
Or in a slightly trust based system, you could simply ask the user whether they installed it manually or from Google Play. If you mention it's to select the update method, and not to prosecute them for installing it from outside Google Play, I have a feeling they'll be happy to tell the truth.
EDIT 3
As @Jan pointed out in the comments, there seems to be a getInstallerPackageName() method in PackageManager which can be used for this purpose.