TelephonyManager.getNetworkCountryIso()在ANDROID 6.0

时间:2017-06-26 01:28:22

标签: android telephonymanager

国家名称null正在使用Android 6.0版本中的TelephonyManager.Below 6.0国家名称正在获取,我也给了权限,但它不起作用。请你帮帮我。谢谢你

private static final String[] PERMISSIONS = {
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.ACCESS_COARSE_LOCATION
};
private TelephonyManager mTelephonyManager;
private EditText et_ind,et_us;
private Button bt_money_convert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);       
    checkPermissions();
}
private void checkPermissions() {
    // Checks the Android version of the device.
    //if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) ||(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)) {
    if (Build.VERSION.SDK_INT >= 23){
        TelephonyManager mTelephonyManager= (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String getCountryName1 =mTelephonyManager.getNetworkCountryIso();
        Toast.makeText(MainActivity.this,"Location--->"+getCountryName1,Toast.LENGTH_LONG).show();

    } else {
        TelephonyManager mTelephonyManager= (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String getCountryName =mTelephonyManager.getNetworkCountryIso();
        Toast.makeText(MainActivity.this,"Location--->"+getCountryName,Toast.LENGTH_LONG).show();

    }
} 

1 个答案:

答案 0 :(得分:0)

我在使用双SIM卡设备/手机时遇到此问题,其中第一张SIM卡被禁用。启用后,我得到了预期的值。一个特殊情况,但可能会帮助处于类似情况的人。

相关问题