短信未被发送

时间:2017-02-05 21:32:54

标签: android android-sms

我想通过短信发送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);

2 个答案:

答案 0 :(得分:0)

确保您拥有阅读和发送短信权限(Marshmallow> =)

Manifest.permission.RECEIVE_SMS
ActivityCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_SMS);

答案 1 :(得分: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);

然后在发送短信或发送短信时注册广播接收器:

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);

    }