0

so i'm new to android development and I was just trying to program a simple app that when open sends a text to a specific number then closes it self. The app works fine except that when I run the app it sends 3 sms's. How can I fix this so it only sends one? Here is my code (The app also requests permision to send_sms):

package com.example.texter;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;


public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("+11234567890", null, "message", null, null);
        finish();

}
}

Thanks in advance.

Abe Zukor
  • 19
  • 3
  • How are you launching this `Activity`? is it the `MAIN` `Activity` declared on your `AndroidManifest.xml`? – Emmanuel Dec 18 '14 at 04:01
  • 5
    Don't kill the process. Just `finish()` the Activity. – Mike M. Dec 18 '14 at 04:09
  • possible duplicate of [How to quit android application programmatically](http://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically) - the solution is a dup, but your problem is slightly different. – Ken Y-N Dec 18 '14 at 04:11
  • 3
    Please listen to @MikeM. Killing your process like that also kills a kitten. – Emmanuel Dec 18 '14 at 04:12
  • Thanks I changed it to Finish() and yes it is the Main Activity declared in the androimanifest.xml – Abe Zukor Dec 18 '14 at 04:16

1 Answers1

0

You can try using an application variable to control if the message has been sent or not. This can be done by deriving Application class and specify the new class in AndroidManifest.xml:

Application class:

public class MainApp extends android.app.Application {
     public boolean isSent = false;
}

AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" android:name=".MainApp" >
.....
</application>

In your MainActivity:

protexted void onCreate(Bundle savedInstanceState) {
    MainApp ma = (MainApp)getApplication();
    if(!ma.isSent) {
        // send sms

        // then set the flag
        ma.isSent = true;
    }

    finish();
}
Tran Nguyen
  • 1,341
  • 10
  • 15
  • this works thanks and it only sends one text. but now when ever you open the app it says sorry texter has stopped on screen. any suggestions? – Abe Zukor Dec 27 '14 at 17:07