问题
I have a requirement that if I run my app in one Android device and then try to run the same app in another Android device I need to check first if its another device or not and if it is then I should continue. Could you please tell me if there is any way in which I can achieve this?
Thank you.
回答1:
you can use this:
telephonyManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
telephonyManager.getSimSerialNumber();
getDeviceId
and getSimSerialNumber
are unique per device, so you can check this values
回答2:
To Achieve This the problem is this :
How can we differentiate, if we are installing app,
first time on 1st device or first time on second device.
so for that we have to use a unique key for app to run
on each different device.
So that before saving shared pref,
we can validate the unique key for that device.
Now Question is that how user will get the unique key? So lets assume application vendor will provide key for each device. but for that, app should have some algorithm to validate the key provided by us.
But Unfortunately, If vendor provide the key, user can use that key to install on another device also.
SO The Final Solution is before first run app Must register device on Some Web Service for Activation.
回答3:
Set a flag in shared preferences in first run
SharedPreferences preferences = getSharedPreferences("PREF_NAME", 0);
boolean onlyonce = preferences.getBoolean("FLAG_NAME", false);
if (!onlyonce ) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("FLAG_NAME", true);
editor.commit();
}
There is a risk if user resets application it gets cleared also. Will this solve your problem?
来源:https://stackoverflow.com/questions/14724949/how-to-detect-another-android-device