1

我想通过短信发送 SIM 卡详细信息,我正在使用此代码(如下所示)但它不起作用(未发送短信),我没有收到任何错误或异常。请问是什么问题?

代码:

SmsManager smsManager = SmsManager.getDefault();

String simDetails = "Your device's SIM Details Are:\n"+"\nDevice ID: "+telephony.getDeviceId()+"\nSubscriber ID: "+telephony.getSubscriberId()
                +"\nSIM Serial Number: "+telephony.getSimSerialNumber()+"\nNetwork Operator: "+telephony.getNetworkOperator()+"\nNetwork Operator Name: "+telephony.getNetworkOperatorName()
                +"\nNetwork Country: "+telephony.getNetworkCountryIso()+"\nSIM Country: "+telephony.getSimCountryIso()+"\nSIM Operator: "+telephony.getSimOperator()+"\nSIM Operator Name: "+telephony.getSimOperatorName()
                +"\nSoftware Version: "+telephony.getDeviceSoftwareVersion()+"\nGroup Id Level-1: "+telephony.getGroupIdLevel1()+"\nMMS UAP: "+telephony.getMmsUAProfUrl()+"\nMMS User Agent: "+telephony.getMmsUserAgent()
                +"\nVoice Mail Tag: "+telephony.getVoiceMailAlphaTag()+"\nVoice Mail Number: "+telephony.getVoiceMailNumber()+"\nLine-1 Number: "+telephony.getLine1Number()+"SIM Location: "+telephony.getCellLocation();

smsManager.sendTextMessage("receiver's number", null, simDetails, null, null);
4

2 回答 2

0

您是否包括了这些权限?

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />

并确保检查它们是否适用于 Android M 以上的设备:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (context.checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
            //All Permissions are granted
            return true;
        } else {
            ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.SEND_SMS}, YOURPERMISSIONRESULTCODEINT);
            //permissions are not granted so this check has failed
            return false;
        }
    }
     //The device is <Android M and so manifest declarations should be enough
    return true;
}

重复此操作或为字符串数组中的每个权限添加单独检查(这将创建一个权限组)。

对于您的网络管理员:

LocationManager locationManager = (LocationManager) context
            .getSystemService(context.LOCATION_SERVICE);
// getting network status
boolean isNetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

一些短信细节:

String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
final SmsManager smsManager = SmsManager.getDefault();
//Allows you to detect when an SMS has been sent via an application wide broadcast
final PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
                new Intent(SENT), 0);
//Same as above but is instead when it is delivered
final PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
                new Intent(DELIVERED), 0);

然后在 SMS 发送后或发送期间注册广播接收器:

context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(context, "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(context, "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(context, "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(context, "Radio off",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        },  new IntentFilter(SENT));

        //---when the SMS has been delivered else this code won't run---
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(context, "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));
        //Then send your message
        smsManager.sendTextMessage("phoneNumber", null, "Message", sentPI, deliveredPI);

    }
于 2017-02-05T22:02:56.870 回答
0

确保您具有读取和发送 SMS 权限(棉花糖 >=)

Manifest.permission.RECEIVE_SMS
ActivityCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_SMS);
于 2017-02-05T21:45:42.143 回答