问题
I would like to get the unique device id on android with Flutter.
I've tried this plugin device_info but it doesn't return the ANDROID_ID that I get in java Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
how can I get the same id?
回答1:
In this case it might be simpler using platform specific code and a Flutter platform channel setup.
You can then use your Java code as included in your question and pass the Android ID to your Flutter application via a platform channel when needed.
See Writing custom platform-specific code with platform channels .
回答2:
You can do what you did, but it is not reliable or guaranteed. Please read up on: https://developer.android.com/training/articles/user-data-ids
for best handling of unique IDs. Of course you can get the phone network ID, but what if that device doesn't have a carrier. You could get the device ID, but what if it returns null for that manufacturer. You could get the Advertiser ID, and this is often recommended, but it CAN CHANGE, it is unlikely to change, but if they do a factory reset or anything like that, it would.
So your best bet might be to generate your own unique IDs and store them locally with the application either in a DB implementation or SharedPreferences.
So to answer your question, if you have existing audience that you used the wrong, bad practice way of using unique IDs, then you should make an ID factory with if/else logic to fix it.
If (first time accessing an ID for this installed application)
//createOne and store it
else if(ID already exists and matches ANDROID_SECURE_ID method)
//get it the old way,
//create new one, and update your access to APIs or DB with new associated ID so that you never hit this code path again
else
//use correct ID implementation that you created
You could also just as easily do a one time check in your Application onCreate to fix anyone with wrong ID and then set a flag that you fixed it so you don't fix it again in sharedpref.
来源:https://stackoverflow.com/questions/50260895/get-unique-android-id-on-flutter-is-it-possible