在我的应用程序中,我想从 SIM卡获取电话号码并显示用户。
我写了以下代码,但是在模拟器中显示了我的SIM卡号,但是某些设备(例如三星)没有显示任何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卡号?
答案 0 :(得分:2)
Add permission in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
and then use this code
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String TAG = "PhoneActivityTAG";
Activity activity = MainActivity.this;
String wantPermission = Manifest.permission.READ_PHONE_STATE;
private static final int PERMISSION_REQUEST_CODE = 1;
ArrayList<String> _mst=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!checkPermission(wantPermission)) {
requestPermission(wantPermission);
} else {
Log.d(TAG, "Phone number: " + getPhone());
_mst = getPhone();
for (String op: _mst) {
Log.i("Device Information", String.valueOf(op));
}
}
}
@TargetApi(Build.VERSION_CODES.O)
private ArrayList<String> getPhone() {
TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(activity, wantPermission) != PackageManager.PERMISSION_GRANTED) {
return null;
}
ArrayList<String> _lst =new ArrayList<>();
_lst.add(String.valueOf(phoneMgr.getCallState()));
_lst.add("IMEI NUMBER :-"+phoneMgr.getImei());
_lst.add("MOBILE NUMBER :-"+phoneMgr.getLine1Number());
_lst.add("SERIAL NUMBER :-"+phoneMgr.getSimSerialNumber());
_lst.add("SIM OPERATOR NAME :-"+phoneMgr.getSimOperatorName());
_lst.add("MEI NUMBER :-"+phoneMgr.getMeid());
_lst.add("SIM STATE :-"+String.valueOf(phoneMgr.getSimState()));
_lst.add("COUNTRY ISO :-"+phoneMgr.getSimCountryIso());
return _lst;
}
private void requestPermission(String permission){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)){
Toast.makeText(activity, "Phone state permission allows us to get phone number. Please allow it for additional functionality.", Toast.LENGTH_LONG).show();
}
ActivityCompat.requestPermissions(activity, new String[]{permission},PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Phone number: " + getPhone());
} else {
Toast.makeText(activity,"Permission Denied. We can't get phone number.", Toast.LENGTH_LONG).show();
}
break;
}
}
private boolean checkPermission(String permission){
if (Build.VERSION.SDK_INT >= 23) {
int result = ContextCompat.checkSelfPermission(activity, permission);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
} else {
return true;
}
}
}
答案 1 :(得分:0)
only use this line of code inside the onCreate method.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<SubscriptionInfo> _sb = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
for (int i = 1; i < _sb.size(); i++) {
SubscriptionInfo info = _sb.get(i);
Log.d(TAG, "Mobile_number " + info.getNumber());
}
}
答案 2 :(得分:0)
使用此代码
public class MainActivity extends AppCompatActivity {
String TAG = "PhoneActivityTAG";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<SubscriptionInfo> _sb = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
for (int i = 1; i < _sb.size(); i++) {
SubscriptionInfo info = _sb.get(i);
Log.d(TAG, "Mobile_number " + info.getNumber());
}
}
}
}
答案 3 :(得分:0)
获取两个SIM卡详细信息。
if (SubscriptionManager.from(this).getActiveSubscriptionInfoCount() > 0) {
ArrayList<String> _lst = new ArrayList<>();
for (int i = 0; i < SubscriptionManager.from(this).getActiveSubscriptionInfoList().size(); i++) {
_lst.add("\n".concat(String.valueOf(phoneMgr.createForSubscriptionId(i).getCallState())));
_lst.add("\nIMEI NUMBER : " + phoneMgr.createForSubscriptionId(i).getImei());
_lst.add("\nMOBILE NUMBER : ".concat(phoneMgr.createForSubscriptionId(i).getLine1Number()));
_lst.add("\nSERIAL NUMBER : ".concat(phoneMgr.createForSubscriptionId(i).getSimSerialNumber()));
_lst.add("\nSIM OPERATOR NAME : " + phoneMgr.createForSubscriptionId(i).getSimOperatorName());
_lst.add("\nMEI NUMBER : " + phoneMgr.createForSubscriptionId(i).getMeid());
_lst.add("\nSIM STATE : ".concat(String.valueOf(phoneMgr.createForSubscriptionId(i).getSimState())));
_lst.add("\nCOUNTRY ISO : " + phoneMgr.createForSubscriptionId(i).getSimCountryIso());
}
return _lst;
}