sendTextMessage looping???

Put your problem here if it does not fit any of the other categories.

sendTextMessage looping???

Postby reynolds » Mon Jan 10, 2011 4:41 am

I tried to send a text message (I got a text message response as well from the service number). But it is repeatedly doing it, as if there’s a loop of sending message happening. How do I make it only send once? I tried adding finish() after sendTextMessage(), but it still sends it.

Code: Select all
package com.eoanlabs.myapp;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;

public class MYAPP extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        sendSMS("123","Hello"); // modified to a dummy number

    //---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, MYAPP.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);


Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MYAPP"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

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

What am I missing? or doing wrong?
Junior Developer
Junior Developer
Posts: 22
Joined: Fri Mar 06, 2009 6:13 am
Location: Makati Philippines


Re: sendTextMessage looping???

Postby Skiller » Mon Jan 10, 2011 9:59 am

I'm not too familiar with pending intents or sms manager so sorry if I'm mistaken, but it looks to me like when you call sendTextMessage you are telling it to run your activity again via the pending intent when the sms is sent, then onCreate is called when the activity starts again which calls sendSMS again which then sets up your app to run again when the next sms is sent which then ends up calling sendSMS again... and so on.

I think just calling "sms.sendTextMessage(phoneNumber, null, message, null, null);" without the pending intent would do what you want.
Posts: 33
Joined: Sun Nov 28, 2010 8:35 pm
Location: Australia


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 4 guests