0

App's basic idea is:

1) BroadcastReceiver, receives an SMS

2) when SMS is received, another activity is called which sends another sms.

The issue is that , when BroadcastReceiver calls the other activity (SMSMessaging) , it's onCreate method gets called again and again , and hence SMS's keeps on being sent again and again . My code is below , Please help me !

This is my BroadcastReciever Class :

package com.example.testing;

import java.util.Arrays;
import java.util.List;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Bundle bundle = intent.getExtras();
        SmsMessage[] recievedMsgs = null;
        String str = "";
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            recievedMsgs = new SmsMessage[pdus.length];
            for (int i = 0; i < recievedMsgs.length; i++) {
                recievedMsgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                str += "SMS from " +
                recievedMsgs[i].getOriginatingAddress() +
                " :" +
                recievedMsgs[i].getMessageBody().toString();
            }
            try {
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

                Intent i = new Intent(context,SMSMessaging.class);  
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                context.startActivity(i);  
                Toast.makeText(context,"Yahoo",Toast.LENGTH_SHORT).show();
            } catch (Exception e) {

            }
        }
    }
}

This is my SMSMessaging Class :

package com.example.testing;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSMessaging extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SMSMessaging.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage("03331234567", null, "auto sending ", pi, null);     
        finish();
    }
}
Darwind
  • 7,284
  • 3
  • 49
  • 48
Faisal
  • 566
  • 5
  • 17
  • I'm far from being an expert on the topic but I'm guessing that it's your PendingIntent causing the trouble. On documentation it says that this intent is being broadcast once message has been sent or sending has failed. Now, could it be so that this broadcasting restarts your SMSMessaging activity over and over again? – harism Apr 14 '13 at 14:15

2 Answers2

0

Simple answer here is, Don't send sms from Activity, send it from BroadCastReceiver instead. I never see finish() calling in onCreate itself.

try calling/sending sendSms from receiver it self.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • Despite the OP's issue, calling `finish()` in `onCreate()` is perfectly legal and Android documentation even specifies what Android should do in this case, namely forward the Activity without rendering it. – class stacker Apr 14 '13 at 14:28
  • @ClassStacker yes, but it is not good approach what OP is using. – AAnkit Apr 14 '13 at 14:41
  • @AndroidEnthusiast : Yes , it worked . but what if i want to send it from another activity ? what should i do then . please guide me – Faisal Apr 14 '13 at 14:41
  • @both : i called finish() just to see if it resolves my issue . i.e. this method stops being called infinitely , but it didn't work . i will remove it ! it is useless right now ! – Faisal Apr 14 '13 at 14:43
  • @user2279715 i dont see any good reason to start a new activity just to send a message, Can you please tell us , why creating new activity to do this? – AAnkit Apr 14 '13 at 14:45
  • this is just starting of my application , the actual idea is that , when sms is recieved , i will get it's contents (which will be some sort of code) and fetch data from a particular website on the basis of that code , and then return that data back to the sender by sms . If i code the whole thing in a single class, it will be quite messed up :( . Secondly i might want to use Delivery,Sent,Failed Intents/Reports using Broadcast Reciever to see the status of my sent sms, and i don't know how to do that in this class :/ – Faisal Apr 14 '13 at 14:58
  • if you need user interaction then only start activity, else a different thread will do the needful. – AAnkit Apr 14 '13 at 15:11
  • can you help me in one more thing. am i making this intent right ? .................. PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, Receiver.class), 0); – Faisal Apr 14 '13 at 15:30
  • yes you can also use PendingIntent.getBroadcast instead of getActivity, but why do you need pendingintent to send SMS..below code will work too SmsManager sm = SmsManager.getDefault(); sm.sendTextMessage("9199191919", null, "TEXT SAMPLE", null, null); – AAnkit Apr 14 '13 at 15:37
-1

it is obvious thing. when you receive any sms ,you are notified on your Receiverand then you are sending new sms from SMSMessaging activity,so the new sms will also notify you on your Receiver and again your SMSMessaging will send new message,so the things will be repeated forever.

Insort,you need to redesign your logic.

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57