短信程序不断向同一个电话号码发送相同的内容

发布于 2024-11-09 00:08:12 字数 3983 浏览 0 评论 0原文

我必须执行一个计划短信程序,一切正常,除了唯一的一个:在我第二次尝试发送短信时,该程序将与第一次相同的内容发送到与第一次相同的电话号码第一次。 这是我的代码:

main_activity, sendBtn:

sendBtn.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try { 
                Intent myIntent = new Intent(main_activity.this, MyAlarmService.class);

                String smsNumber = phoneNo.getText().toString();
                String smsText = edit.getText().toString();

                if (smsNumber.length() != 0 && smsText.length() != 0){


                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("extraSmsNumber", smsNumber);
                    bundle.putCharSequence("extraSmsText", smsText);
                    myIntent.putExtras(bundle);
                    String text = mTimeDisplay.getText().toString() + " " + mDateDisplay.getText().toString();


                    DateFormat formatter ; 
                    Date date ; 
                    formatter = new SimpleDateFormat("HH:mm dd/MM/yyyy");
                    date = (Date)formatter.parse(text);


                    Calendar setCalendar = Calendar.getInstance();
                    setCalendar.setTime(date);


                    pendingIntent = PendingIntent.getService(main_activity.this, 0, myIntent, 0);

                    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                    Calendar calendar = Calendar.getInstance();                

                    calendar.setTimeInMillis(System.currentTimeMillis());
                    alarmManager.set(AlarmManager.RTC_WAKEUP, setCalendar.getTimeInMillis(), pendingIntent);

                    Toast.makeText(main_activity.this,
                            "Start Alarm with \n" +
                            "smsNumber = " + smsNumber + "\n" +
                            "smsText = " + smsText + "\n Contain : " + text,
                            Toast.LENGTH_LONG).show();

                }
                else {
                    showDialog(ALERT_DIALOG_ID);
                }
            }
            catch (ParseException e)
            {Toast.makeText(main_activity.this,"Error!!!",Toast.LENGTH_LONG).show();    }
        }
    }
    );

MyAlarmService:

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MyAlarmService extends Service {

String smsNumberToSend, smsTextToSend;

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
    return null;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    Bundle bundle = intent.getExtras();
    smsNumberToSend = (String) bundle.getCharSequence("extraSmsNumber");
    smsTextToSend = (String) bundle.getCharSequence("extraSmsText");

    sendSMS(smsNumberToSend, smsTextToSend);
}

private void sendSMS(String phoneNumber, String message)
{   
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);    

}

@Override
public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
    return super.onUnbind(intent);
}

}

我真的需要帮助,请帮助我尽快找到解决方案,非常感谢

I'm having to do a schedule SMS program, everything works OK, except the only one: in the second time i try to send SMS, the program send the same content as in the first one, to the same phone number as in the first time.
Here's my code:

main_activity, sendBtn:

sendBtn.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try { 
                Intent myIntent = new Intent(main_activity.this, MyAlarmService.class);

                String smsNumber = phoneNo.getText().toString();
                String smsText = edit.getText().toString();

                if (smsNumber.length() != 0 && smsText.length() != 0){


                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("extraSmsNumber", smsNumber);
                    bundle.putCharSequence("extraSmsText", smsText);
                    myIntent.putExtras(bundle);
                    String text = mTimeDisplay.getText().toString() + " " + mDateDisplay.getText().toString();


                    DateFormat formatter ; 
                    Date date ; 
                    formatter = new SimpleDateFormat("HH:mm dd/MM/yyyy");
                    date = (Date)formatter.parse(text);


                    Calendar setCalendar = Calendar.getInstance();
                    setCalendar.setTime(date);


                    pendingIntent = PendingIntent.getService(main_activity.this, 0, myIntent, 0);

                    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                    Calendar calendar = Calendar.getInstance();                

                    calendar.setTimeInMillis(System.currentTimeMillis());
                    alarmManager.set(AlarmManager.RTC_WAKEUP, setCalendar.getTimeInMillis(), pendingIntent);

                    Toast.makeText(main_activity.this,
                            "Start Alarm with \n" +
                            "smsNumber = " + smsNumber + "\n" +
                            "smsText = " + smsText + "\n Contain : " + text,
                            Toast.LENGTH_LONG).show();

                }
                else {
                    showDialog(ALERT_DIALOG_ID);
                }
            }
            catch (ParseException e)
            {Toast.makeText(main_activity.this,"Error!!!",Toast.LENGTH_LONG).show();    }
        }
    }
    );

MyAlarmService:

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MyAlarmService extends Service {

String smsNumberToSend, smsTextToSend;

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
    return null;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    Bundle bundle = intent.getExtras();
    smsNumberToSend = (String) bundle.getCharSequence("extraSmsNumber");
    smsTextToSend = (String) bundle.getCharSequence("extraSmsText");

    sendSMS(smsNumberToSend, smsTextToSend);
}

private void sendSMS(String phoneNumber, String message)
{   
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);    

}

@Override
public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
    return super.onUnbind(intent);
}

}

I really need help, plz help me find a solution as fast as possible, thx a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2024-11-16 00:08:12

您的问题在于:

pendingIntent = PendingIntent.getService(main_activity.this, 0, myIntent, 0);

首先您需要添加一个标志,以便创建的 PendingIntent 不会每次都被重用。

然后你需要添加一些唯一的请求代码,如果没有,之前的PendingIntent将被取消并替换。即,如果您安排发送两条短信,则只会发送最后一条。

pendingIntent = PendingIntent.getService(main_activity.this, SOMETHING_UNIQUE, myIntent, PendingIntent.FLAG_ONE_SHOT);

Your problem lies here:

pendingIntent = PendingIntent.getService(main_activity.this, 0, myIntent, 0);

First you need to add a flag so the created PendingIntent wont be reused every time.

Then you need to add some unique request code, if not the previous PendingIntent will be canceled and replaced. I.e., if you would schedule two text messages to be sent only the last one would be sent.

pendingIntent = PendingIntent.getService(main_activity.this, SOMETHING_UNIQUE, myIntent, PendingIntent.FLAG_ONE_SHOT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文