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

Hi,
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.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;

public class MYAPP extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sendSMS("123","Hello"); // modified to a dummy number
        finish();
    }

    //---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="http://schemas.android.com/apk/res/android"
      package="com.eoanlabs.myapp"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MYAPP"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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


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

Top

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.
Skiller
Developer
Developer
 
Posts: 33
Joined: Sun Nov 28, 2010 8:35 pm
Location: Australia

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 20 guests