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开发者_C百科 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);
}
}
}
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.
精彩评论