-1

Okay so here is my source code.. Something keeps going wrong and getting an Force close error. Here is the code, can someone tell me if i need to make any changes..

I have 2 classes one that sends the message and one that recieves... Here we go..

My first classes that has a method to send a text..

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class InfoSender extends Activity {
private smsListener smsReceiver;
public String phoneNumber;
public String message; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
}

    public void send(String phoneNumber, String message){

        phoneNumber = smsListener.phnNumber;

          PendingIntent pi = PendingIntent.getActivity(this, 0,
                    new Intent(this, InfoSender.class), 0);                
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, pi, null);

            Toast.makeText(getApplicationContext(), "Details about house "+housenumber+"sent", 6);



            //Do nothing
        }

    }

Second Class that receives and responds automatically if a text says a certain thing..

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String msg = "";

    if(bundle != null){

    }
    Object[]pdus = (Object[])bundle.get("pdus");
    msgs =new SmsMessage[pdus.length];

    for(int i=0; i<msgs.length; i++){
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
        msg += "Sms from "+msgs[i].getDisplayOriginatingAddress();

        phnNumber = msgs[i].getOriginatingAddress();
        msgBody = msgs[i].getMessageBody().toString(); 
    }

        if(msgBody.equalsIgnoreCase("0")){
            String phoneNumber = phnNumber;
            String message = sender.message;

        sender.send(phoneNumber, message);
    }
    }

}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
theITRanger22
  • 184
  • 6
  • 19
  • It would help if you could post your logcat logs to see what the error is exactly. You can enable logcat in eclipse by going to (in the menu bar): Window->Show View->Other->Android->Logcat. – Ryan Jun 23 '11 at 00:44
  • 2
    without describing the context of this code or the app you are making makes this really hard to follow. – Phil Jun 23 '11 at 01:22

1 Answers1

0

Without any logs it's difficult to say, but most likely you have a permission. You need to have permissions set in your manifest to Send, Receive, and Read, and Write.

spierce7
  • 14,797
  • 13
  • 65
  • 106