SMS Manager send mutlipart message when there is less than 160 characters

前端 未结 2 1551
轮回少年
轮回少年 2020-11-28 15:46

I write a app which use SMS Manager. I use method sendTextMessage() but it isn\'t work. Now I am using sendMutlipartTextMessage() and it\'s work. B

2条回答
  •  -上瘾入骨i
    2020-11-28 15:53

    I add my SMS method if it can help someone.

    //TO USE EveryWhere

    SMSUtils.sendSMS(context, phoneNumber, message);
    

    //Manifest

    
    
    
    
     
         
             
             
          
     
    

    //JAVA

    public class SMSUtils extends BroadcastReceiver {
    
        public static final String SENT_SMS_ACTION_NAME = "SMS_SENT";
        public static final String DELIVERED_SMS_ACTION_NAME = "SMS_DELIVERED";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //Detect l'envoie de sms
            if (intent.getAction().equals(SENT_SMS_ACTION_NAME)) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK: // Sms sent
                        Toast.makeText(context, context.getString(R.string.sms_send), Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE: // generic failure
                        Toast.makeText(context, context.getString(R.string.sms_not_send), Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE: // No service
                        Toast.makeText(context, context.getString(R.string.sms_not_send_no_service), Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU: // null pdu
                        Toast.makeText(context, context.getString(R.string.sms_not_send), Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF: //Radio off
                        Toast.makeText(context, context.getString(R.string.sms_not_send_no_radio), Toast.LENGTH_LONG).show();
                        break;
                }
            }
            //detect la reception d'un sms
            else if (intent.getAction().equals(DELIVERED_SMS_ACTION_NAME)) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, context.getString(R.string.sms_receive), Toast.LENGTH_LONG).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(context, context.getString(R.string.sms_not_receive), Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }
    
        /**
         * Test if device can send SMS
         * @param context
         * @return
         */
        public static boolean canSendSMS(Context context) {
            return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        }
    
        public static void sendSMS(final Context context, String phoneNumber, String message) {
    
            if (!canSendSMS(context)) {
                Toast.makeText(context, context.getString(R.string.cannot_send_sms), Toast.LENGTH_LONG).show();
                return;
            }
    
            PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT_SMS_ACTION_NAME), 0);
            PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED_SMS_ACTION_NAME), 0);
    
            final SMSUtils smsUtils = new SMSUtils();
            //register for sending and delivery
            context.registerReceiver(smsUtils, new IntentFilter(SMSUtils.SENT_SMS_ACTION_NAME));
            context.registerReceiver(smsUtils, new IntentFilter(DELIVERED_SMS_ACTION_NAME));
    
            SmsManager sms = SmsManager.getDefault();
            ArrayList parts = sms.divideMessage(message);
    
            ArrayList sendList = new ArrayList<>();
            sendList.add(sentPI);
    
            ArrayList deliverList = new ArrayList<>();
            deliverList.add(deliveredPI);
    
            sms.sendMultipartTextMessage(phoneNumber, null, parts, sendList, deliverList);
    
            //we unsubscribed in 10 seconds 
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    context.unregisterReceiver(smsUtils);
                }
            }, 10000);
    
        }
    }
    

提交回复
热议问题