我如何解决代码使号码电话在我的真实设备上运行?

时间:2019-02-28 07:43:03

标签: android phone-number telephony

我编码了该项目(minSDKVersion:15)以获取电话号码,当我在android studio(API 26)的AVD上运行时,它运行正常。但是,当我在真实的设备(Android版本:4.4.2)上运行时,我单击了“单击我”按钮,然后它崩溃了,并且提示“不​​幸的是,我的应用已停止”。我不知道我的设备不是合适的版本,还是什么问题? 我的代码:

Button btnStart;
TextView varText;
String info;
String strPhoneType = "";
static final int PERMISSION_READ_STATE = 123;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void Start(View view) {
    int permissionClick = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
    if (permissionClick == PackageManager.PERMISSION_GRANTED) {
        MyTelephonyManager();
    } else {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_READ_STATE);
    }
}

覆盖权限方法以请求读取电话号码

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case PERMISSION_READ_STATE:
        {
            if(grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                MyTelephonyManager();
            } else {
                Toast.makeText(this, "You don't have required permisson to make the Action", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

获取电话信息的方法,包括电话号码:

private void MyTelephonyManager(){
    TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    int phoneType = manager.getPhoneType();
    switch (phoneType){
        case (TelephonyManager.PHONE_TYPE_CDMA):
            strPhoneType = "CDMA";
            break;
        case (TelephonyManager.PHONE_TYPE_GSM):
            strPhoneType = "GSM";
            break;
        case (TelephonyManager.PHONE_TYPE_NONE):
            strPhoneType = "None";
            break;
    }

    boolean isRoaming = manager.isNetworkRoaming();
    String PhoneType = strPhoneType;
    String IMEINumber = manager.getImei();
    String subcriberID = manager.getSubscriberId();
    String simSerialNumber = manager.getSimSerialNumber();
    String networkCountryISO = manager.getNetworkCountryIso();
    String simCountryISO = manager.getSimCountryIso();
    String softwareVersion = manager.getDeviceSoftwareVersion();
    String voiceMailNumber = manager.getVoiceMailNumber();

    info = "Phone details: \n";
    info += "\n Phone Network type: " + PhoneType;
    info += "\n IMEINumber: " + IMEINumber;
    info += "\n subcriberID: " + subcriberID;
    info += "\n simSerialNumber: " + simSerialNumber;
    info += "\n networkCountryISO: " + networkCountryISO;
    info += "\n simCountryISO: " + simCountryISO;
    info += "\n softwareVersion: " + softwareVersion;
    info += "\n voiceMailNumber: " + voiceMailNumber;
    info += "\n isRoaming: " + isRoaming;

    btnStart = (Button) findViewById(R.id.idBtnStart);
    varText = (TextView) findViewById(R.id.idTxtView);
    varText.setText(info);
}

0 个答案:

没有答案