我可以自动发送短信(无需用户需要批准)

时间:2011-10-01 12:15:22

标签: android android-activity sms

我对Android很新。 我试图从Android应用程序发送短信。 使用SMS Intent时,SMS窗口打开,用户需要批准SMS并发送它。

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

谢谢, 利奥尔

5 个答案:

答案 0 :(得分:39)

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

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正在等待意图他们可以在方法完成发送短信时触发广播

以下是广播接收器的示例代码

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

您可以在活动

中使用以下行注册
registerReceiver(receiver, new IntentFilter(SMS_SENT));  // SMS_SENT is a constant

另外,不要忘记在onDestroy

中取消注册广播
@Override
protected void onDestroy() {
    unregisterReceiver(receiver);
    super.onDestroy();
}

答案 1 :(得分:18)

如果您的应用程序在AndroidManifest.xml中具有以下权限

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

您可以使用

发送任意数量的短信
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(...);

就是这样。

答案 2 :(得分:7)

是的,您可以使用SmsManager发送短信。请注意,您的申请需要SEND_SMS权限才能生效。

答案 3 :(得分:2)

是的,您可以在不进行用户交互的情况下发送短信...但是当用户想要将短信发送到单个号码时,它可以正常工作。

UIApplication.shared.statusBarStyle = .lightContent

UINavigationBar.appearance().clipsToBounds = true

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView

statusBar.backgroundColor = UIColor.orange

另外,添加清单权限....

try {    
    SmsManager.getDefault().sendTextMessage(RecipientNumber, null,    
    "Hello SMS!", null, null);    
} catch (Exception e) {    
    AlertDialog.Builder alertDialogBuilder = new    
    AlertDialog.Builder(this);    
    AlertDialog dialog = alertDialogBuilder.create();    
    dialog.setMessage(e.getMessage());    
    dialog.show();    
}    

答案 4 :(得分:0)

//sent sms
private void sendSMS(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {

            switch (getResultCode()) {

                case Activity.RESULT_OK:

                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:

                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_NULL_PDU:

                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_RADIO_OFF:

                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    // ---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {

            switch (getResultCode()) {

                case Activity.RESULT_OK:

                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;

                case Activity.RESULT_CANCELED:

                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

您可以使用此完整功能