java 如何在 Android 中以编程方式获取 IMSI 编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30212163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically get the IMSI-Number in Android
提问by AndyB
I tried the following code:
我尝试了以下代码:
private String getImsi() {
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getSubscriberId();
}
<uses-permission android:name='android.permission.READ_PHONE_STATE' />
But it only returns null.
Is there any other way to obtain the IMSI with java in android?
但它只返回空值。
有没有其他方法可以在android中使用java获取IMSI?
采纳答案by Ankit Kumar
String imei = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);
with permission
经许可
android.permission.READ_PHONE_STATE
Since SystemProperties is a hidden class in Android, you can access it with reflection:
由于 SystemProperties 在 Android 中是一个隐藏类,因此可以通过反射访问它:
/**
* Get the value for the given key.
* @return an empty string if the key isn't found
*/
public static String get(Context context, String key) {
String ret = "";
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[1];
paramTypes[0]= String.class;
Method get = SystemProperties.getMethod("get", paramTypes);
//Parameters
Object[] params = new Object[1];
params[0] = new String(key);
ret = (String) get.invoke(SystemProperties, params);
} catch(Exception e) {
ret = "";
//TODO : Error handling
}
return ret;
}
回答by End.Game
Did you add this permission in the manifest?
您是否在清单中添加了此权限?
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Edit: ok, try use this one:
编辑:好的,尝试使用这个:
String myIMSI =
android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);
SystemProperties it's an hidden class. try to check here: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/SystemProperties.java
SystemProperties 它是一个隐藏类。尝试在这里检查:https: //android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/SystemProperties.java