Can i automatically send SMS (Without the user need to approve)

前端 未结 5 634
迷失自我
迷失自我 2020-12-02 08:36

I\'m rather new to Android. Im trying to send SMS from Android application. When using the SMS Intent the SMS window opens and the user needs to approve the SMS and send i

5条回答
  •  悲哀的现实
    2020-12-02 08:54

    You can use this method to send an sms. If the sms is greater than 160 character then sendMultipartTextMessage is used.

    private void sendSms(String phonenumber,String message, boolean isBinary)
    {
        SmsManager manager = SmsManager.getDefault();
    
        PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
        PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
    
        if(isBinary)
        {
                byte[] data = new byte[message.length()];
    
                for(int index=0; index MAX_SMS_MESSAGE_LENGTH)
                {
                        ArrayList messagelist = manager.divideMessage(message);
    
                        manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
                }
                else
                {
                        manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
                }
        }
    }
    

    Update

    piSend and piDelivered are Pending Intent They can trigger a broadcast when the method finish sending an SMS

    Here is sample code for broadcast receiver

        private BroadcastReceiver receiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                String message = null;
    
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    message = "Message sent!";
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    message = "Error. Message not sent.";
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    message = "Error: No service.";
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    message = "Error: Null PDU.";
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    message = "Error: Radio off.";
                    break;
                }
    
                AppMsg.makeText(SendMessagesWindow.this, message,
                        AppMsg.STYLE_CONFIRM).setLayoutGravity(Gravity.BOTTOM)
                        .show();
          }
      };
    

    and you can register it using below line in your Activity

    registerReceiver(receiver, new IntentFilter(SMS_SENT));  // SMS_SENT is a constant
    

    Also don't forget to unregister broadcast in onDestroy

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }
    

提交回复
热议问题