使用onRequestPermissionsResult以编程方式获取设备IMEI号码android java的最佳方法[重复]
Posted
技术标签:
【中文标题】使用onRequestPermissionsResult以编程方式获取设备IMEI号码android java的最佳方法[重复]【英文标题】:Best Way to get Device IMEI number android java programmatically with onRequestPermissionsResult [duplicate] 【发布时间】:2018-07-11 09:54:54 【问题描述】:************************************我找到的最好方法:********** ****************************** ******************************成功完成************************ *************** *****************************只需复制以下代码********************** ********************
清单:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
JAVA: 在 onCreate 中:
String deviceIMEI;
CheckPermissionAndStartIntent();
在根目录中:
private void CheckPermissionAndStartIntent()
if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED)
ActivityCompat.requestPermissions(SplashActivity.this, new String[]Manifest.permission.READ_PHONE_STATE, 1);
//SEY SOMTHING LIKE YOU CANT ACCESS WITHOUT PERMISSION
else
doSomthing();
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
switch (requestCode)
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
doSomthing();
else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED)
//SEY SOMTHING LIKE YOU CANT ACCESS WITHOUT PERMISSION
//you can show something to user and open setting -> apps -> youApp -> permission
// or unComment below code to show permissionRequest Again
//ActivityCompat.requestPermissions(MainActivity.this, new String[]Manifest.permission.READ_PHONE_STATE, 1);
doSomthing()
deviceIMEI = getDeviceIMEI(MainActivity.this);
//andGoToYourNextStep
@SuppressLint("HardwareIds")
public static String getDeviceIMEI(Activity activity)
String deviceUniqueIdentifier = null;
TelephonyManager tm = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
if (null != tm)
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(activity, new String[]Manifest.permission.READ_PHONE_STATE, 1);
else
deviceUniqueIdentifier = tm.getDeviceId();
if (null == deviceUniqueIdentifier || 0 == deviceUniqueIdentifier.length())
deviceUniqueIdentifier = "0";
return deviceUniqueIdentifier;
【问题讨论】:
我看到了那个主题,但我找不到我的结果 【参考方案1】:请使用这个
清单文件
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
在 Java 文件中
public static String getUniqueIMEIId(Context context)
try
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return "";
String imei = telephonyManager.getDeviceId();
Log.e("imei", "=" + imei);
if (imei != null && !imei.isEmpty())
return imei;
else
return android.os.Build.SERIAL;
catch (Exception e)
e.printStackTrace();
return "not_found";
【讨论】:
thanQ 但那是返回 null --> ( " " ) 您是否正在检查 Android 设备? 是的,我的设备是真实的,不是虚拟设备:'( 好的。你在你的应用程序中允许运行时权限吗? 使用:字符串imei = telephonyManager.getImei();字符串序列 = android.os.Build.getSerial();对于新版本的 android sdks(例如我目前使用的 API 27)【参考方案2】:试试这个
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
return;
String imei = telephonyManager.getDeviceId();
在 Manifest.xml 中添加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
【讨论】:
【参考方案3】:public String getIMEI(final Context context)
final TelephonyManager ts =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return ts.getDeviceId()) ;
确保您拥有 Manifest.permission.READ_PHONE_STATE 权限
【讨论】:
以上是关于使用onRequestPermissionsResult以编程方式获取设备IMEI号码android java的最佳方法[重复]的主要内容,如果未能解决你的问题,请参考以下文章
在使用加载数据流步骤的猪中,使用(使用 PigStorage)和不使用它有啥区别?
Qt静态编译时使用OpenSSL有三种方式(不使用,动态使用,静态使用,默认是动态使用)