Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7504571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:37:06+00:00 2026-05-29T21:37:06+00:00

Hey all of you at Stackoverflow. I got this incredibly annoying problem. I try

  • 0

Hey all of you at Stackoverflow.

I got this incredibly annoying problem. I try to build a SMS application which will send an SMS at a specific time using AlarmManager. And funny enough (cause it seemed the hardest) sending the SMS at a specific time works flawlessly,
but what thrills (or kills) me is that it can only send a SMS after I’ve clicked the Button twice?!

Anybody got an idea cause I’m soon going out of my mind here, been trying for hours.

The code below is the code for the Button.click event:

   public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub  
                //txtClear();

                try {

                    Intent myIntent = new Intent(SMSAlarm.this, MyAlarmService.class);
                    smsNumber = txtPhoneNo.getText().toString();
                    smsText = txtMessage.getText().toString();

//                  if(smsNumber.length()<=0 || smsText.length()<=0)
//                  {
//                      Toast.makeText(getBaseContext(),
//                              "Please enter both phone number and message",
//                              Toast.LENGTH_SHORT).show();
//                  }
//                  else
//                  {

                    TimePicker timePickerAlarm = (TimePicker) findViewById(R.id.timePickerAlarm);
                    DatePicker datePick = (DatePicker) findViewById(R.id.datePickerAlarm);

                    Calendar calendar = calendarInformation(timePickerAlarm,
                            datePick);

                    Calendar today = Calendar.getInstance();

                    //Check if chosen date is less than the current date and time
                    //IF True, inform the user that the selected date has passed
                    //ELSE, start the service for sending SMS

//                  if(calendar.getTime().getTime() <= today.getTime().getTime())
//                  {
//                      Toast.makeText(getBaseContext(), "Date has passed!\nPlease choose a new date", Toast.LENGTH_LONG).show();
//                  }
//                  else
//                  {   

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

                        Bundle bundle = new Bundle();
                        bundle.putCharSequence("extraSmsNumber", smsNumber);
                        bundle.putCharSequence("extraSmsText", smsText);
                        myIntent.putExtras(bundle);
                        requestCode+= 1;
                        //Temporary solution. Added to make the requestCode unique, to be able to send more than one SMS in a row with different text and to a different number.
                        pendingIntent = PendingIntent.getService(SMSAlarm.this, requestCode, myIntent, PendingIntent.FLAG_ONE_SHOT); //PendingIntent.FLAG_ONE_SHOT
                        //pendingIntent = PendingIntent.getService(SMSAlarm.this, 0, myIntent, 0);  
                        //}
                    //}
                    txtClear();
                }                                    
                catch (Exception e) 
                {
                    // TODO Auto-generated catch block
                    Toast.makeText(getBaseContext(), "Error sending SMS " + e.getMessage(), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                    txtClear();
                }
              }

And here is the MyAlarmService class:

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
        try {
            super.onStart(intent, startId);     

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

            Toast.makeText(this, "Sending Message...", Toast.LENGTH_LONG).show();
            Toast.makeText(this,
                    "Number = " + smsNumberToSend + "\n" +
                    "Message = " + smsTextToSend,               
                    Toast.LENGTH_LONG).show();

            sendSMS(smsNumberToSend, smsTextToSend);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private void sendSMS(String phoneNumber, String message) {
        // TODO Auto-generated method stub
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.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);      
    }
}

Any help is much appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:37:10+00:00Added an answer on May 29, 2026 at 9:37 pm
              pendingIntent = PendingIntent.getService(SMSAlarm.this, requestCode, myIntent,
                                    PendingIntent.FLAG_ONE_SHOT); 
    

    change PendingIntent.FLAG_ONE_SHOT as myIntent.FLAG_ACTIVITY_NEW_TASK

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. Ive got a headache trying to get my head around this. I
hey all.i'm newbie at this problem.i have this data in table result: item range_code
Hey all this is the first time i am calling a stored procedure via
Hey stackoverflow - This is my first question here. I have 2 tables in
Hey all. I've been been trying to figure this out for a while now.
Hey all, just wondering what the best way to add this capability was. I
Hey all- I have looked this up on here and Google but none of
hey all, throwing this one out there... hope it's a simple one. using the
Hey all, this is my query string here: SELECT SUM(Total) as Total, AdministratorCode, SUM(WOD.Quantity)
Hey all, my Computational Science course this semester is entirely in Java. I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.