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

  • SEARCH
  • Home
  • 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 8523193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:20:19+00:00 2026-06-11T07:20:19+00:00

The code (android application) I am developing receives an SMS and if the SMS

  • 0

The code (android application) I am developing receives an SMS and if the SMS is from a certain number, then it returns a predefined SMS. This all works fine, though I want to add a Timer so that the reply seems realistic. Through a bit of research I have come up with the code below to try and delay the code so that the message sends after 30 second delay.

I define a new timer called Timer1, a delay of 30 seconds and a TimerTask called tt1 at the top, then in the onReceive( ), just before I call the sendSMS method I call the timer() method which starts the timer.

However, the Timer never stops and the message never sends. The app eventually crashes after a few Can anyone see what I am doing wrong?

    static Timer timer1 = new Timer();
static long delay = 30000;
static TimerTask tt1;

        private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        //---gather up all the necessary user input---
        prefs = getSharedPreferences(prefName, MODE_PRIVATE);
        String textMessage = (String) prefs.getString(NAME_KEY, "");
        final Button btn2 = (Button)findViewById(R.id.btnContacts);
        String phoneNumber = (String) btn2.getText();
        String Sender = (String) intent.getExtras().getString("Sender");

        if(Sender.equals(phoneNumber))
        {
            timer();
            sendSMS(phoneNumber, textMessage); 
        }
        }
    }
     };

        //---holds the delay for realistic reply time---
public static void timer() 
{
    timer1.schedule(tt1 ,0,delay);      
}




//---sends an SMS message to another device---
public void sendSMS(String phoneNumber, String message)
{       
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

Edit: I am now using handler.postDelayed to pause on sending the text. This works much better

Good beginner resource for handler.postDelayed found here: http://www.appaholics.in/running-a-task-with-a-delay/

  • 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-06-11T07:20:21+00:00Added an answer on June 11, 2026 at 7:20 am

    use Handler.postDelayed instead http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,%20long)

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

Sidebar

Related Questions

I am developing an android app using code from a normal java application. In
I have been developing Android application where I use this code: Date d=new Date(new
I'm developing an Android 2.3.3 application with Java. This app is an iOS code
I used this code in my Android application: import org.apache.http.client.methods.HttpPost; ... HttpClient httpclient =
I'm trying to connecting SQL Azure from my android application. The code is working
I've borrowed the following code from Wei-Meng Lee's "Beginning Android Application Development": import android.content.Context;
I am developing an Android application, I was doing just fine then I made
I am developing an Android Application. In this application, Logo bar is shown on
I have been developing the application which must catch all input sms. For it
I am developing an android application where i have no errors in the code.But

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.