【发布时间】:2018-12-17 22:28:51
【问题描述】:
在我的应用程序中,我想从 sim 卡 获取 电话号码 并显示用户。
我写了下面的代码,但在模拟器中显示我的 sim 卡号,但某些设备如 Samsung 没有显示任何 sim 卡号。
清单代码:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
活动代码:
public class FetchSimActivity extends AppCompatActivity {
TextView fetchSimCardTxt, fetchNumCardTxt;
TelephonyManager tm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fetch_sim);
fetchSimCardTxt = findViewById(R.id.fetchSimCardTxt);
fetchNumCardTxt = findViewById(R.id.fetchNumCardTxt);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, 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 simID = tm.getSimSerialNumber();
if (simID != null) {
fetchSimCardTxt.setText("SIM ID : + " + simID);
}
String telNumber = tm.getLine1Number();
if (telNumber != null) {
fetchNumCardTxt.setText("SIM NUM : + " +telNumber);
}
}
}
我如何从android中的所有设备获取sim卡号?
【问题讨论】: