我可以自动发送短信吗(无需用户批准) [英] Can i automatically send SMS (Without the user need to approve)

查看:18
本文介绍了我可以自动发送短信吗(无需用户批准)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 比较陌生.我正在尝试从 Android 应用程序发送短信.使用 SMS Intent 时,SMS 窗口会打开,用户需要批准 SMS 并发送.

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 it.

有没有办法在没有用户确认的情况下自动发送短信?

Is there a way to automatically send the SMS without the user confirming it?

谢谢,利奥

推荐答案

您可以使用此方法发送短信.如果 sms 大于 160 个字符,则使用 sendMultipartTextMessage.

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<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
            {
                    data[index] = (byte)message.charAt(index);
            }

            manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);
    }
    else
    {
            int length = message.length();

            if(length > MAX_SMS_MESSAGE_LENGTH)
            {
                    ArrayList<String> messagelist = manager.divideMessage(message);

                    manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
            }
            else
            {
                    manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
            }
    }
}

更新

piSend 和 piDelivered 是 Pending Intent,当方法发送完一条短信后可以触发广播

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

也不要忘记在onDestroy中注销广播

Also don't forget to unregister broadcast in onDestroy

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

这篇关于我可以自动发送短信吗(无需用户批准)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆