Generating Device-Specific Serial Number [duplicate]

吃可爱长大的小学妹 提交于 2020-01-09 19:07:30

问题


I would like to be able to generate unique serial numbers for each Android device for use in unlocking an application. How could I do this?

EDIT:

The reason is I want to revamp a paid application and provide users who have paid for the old version, which will have a different package name, a way to obtain the full version by downloading an unlockable free version of the application. I would push an update to the old version that would generate and display the code that they could enter to turn the Free version into a fully functional version.


回答1:


You can use the Android id. This id should be unique to devices, but how it is set depends on the implementation of the device manufacturer.

String deviceId = Secure.getString(context.getContentResolver(),Secure.ANDROID_ID);

The Android Id may change on factory reset of the phone and the user is also able to change it on rooted phones. But if you need an id to identify your user it should be fine.




回答2:


why not using ther google account name? is easy to get and needs only a simple request on the manifest file. they will have purchased the license with gplay, so the g+ account name should be enough...

in the manifest:

<manifest ... >
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    ...
</manifest>

to retrieve the account name:

AccountManager am = AccountManager.get(this); // "this" references the current Context

Account[] accounts = am.getAccountsByType("com.google");

to retrieve the name:

accounts[0].name

i write a simple alert to make me sure i have found an account here the whole code:

Account[] accounts = am.getAccountsByType("com.google");
    AlertDialog.Builder miaAlert = new AlertDialog.Builder(this);

    miaAlert.setTitle("i found an account name!");
    miaAlert.setMessage(accounts[0].name);
    AlertDialog alert = miaAlert.create();
    alert.show();



回答3:


Android already provides a licensing service for use by paid apps. Is there a reason you don't want to use this? Bear in mind that trying to lock your app to a particular phone is going to really annoy users who switch devices (Eg, all of them, sooner or later).




回答4:


From Google Developer's solution in solving issue with PRNG, getting unique device serial number via reflection:

http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html

/**
 * Gets the hardware serial number of this device.
 *
 * @return serial number or {@code null} if not available.
 */
private static String getDeviceSerialNumber() {
    // We're using the Reflection API because Build.SERIAL is only available
    // since API Level 9 (Gingerbread, Android 2.3).
    try {
        return (String) Build.class.getField("SERIAL").get(null);
    } catch (Exception ignored) {
        return null;
    }
}



回答5:


You can use the IMEI no for this.




回答6:


You can use Serial Number as a unique Identifier as they are availbalble in all devices.You should not use IMEI as a unique identifier as they are not abalibale in WIFI Tabs(tabs that do not have SIM Card Slot).

You can use

    try {
        Class<?> c = Class.forName("android.os.SystemProperties");
        Method get = c.getMethod("get", String.class, String.class);
        serial = (String) get.invoke(c, "ril.serialnumber", "unknown");
    } catch (Exception ignored) {
    }


来源:https://stackoverflow.com/questions/3464881/generating-device-specific-serial-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!