我如何在Android应用程序中重复发送文本

时间:2015-06-24 20:46:57

标签: android sms messaging

我正在按照教程使用sms消息,我想在用户点击发送按钮时连续发送4次消息,但我不确切知道如何。我已经尝试在代码的不同部分放置for循环但它不起作用。

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SMS extends Activity {

    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);


        btnSendSMS.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString();
                if(phoneNo.length()>0 && message.length()>0){
                     for(int i=0; i<5; i++){
                    sendSMS(phoneNo, message);} //this is one of the places where iv'e placed a for loop
                }
                else{
                    Toast.makeText(getBaseContext(), "Please enter both phone number and message.", Toast.LENGTH_SHORT).show();}
            }
        });
    }

    private void sendSMS(String phoneNumber, String message)
    {


        PendingIntent pi = PendingIntent.getActivity(this, 0,new Intent(this, SMS.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);

    }

1 个答案:

答案 0 :(得分:0)

我不认为这是可能的。在Android上的指定时间段内可以发送的消息数量有限制。这就是为什么循环不起作用的原因 - Android正在阻止它。据我记忆,限制是30分钟内30条消息。

此限制有解决方法,但它们都只能通过手机内的命令shell访问。

相关问题