I’m working on an Android app and I need to to post to a PHP form or insert into a MySQL database when an incoming message is received. Some of you may be hesitating to respond, but let me assure you that this isn’t a malicious app.
The code that I’m posting below is what I’m using to block incoming messages, except from one number. Would anyone be able to help me figure out a way to trigger a post to my server when the text message is blocked?
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 SmsReceiver extends BroadcastReceiver {
public static int MSG_TPE=0;
private String getAddress;
public void onReceive(Context context, Intent intent) {
String MSG_TYPE=intent.getAction();
if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
Toast received = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
received.show();
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
getAddress = smsMessage[0].getOriginatingAddress();
if(getAddress.equals("APPROVEDPHONENUMBER")) {
Toast approved = Toast.makeText(context,"Approved SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG);
approved.show();
// Message is approved and let through
} else {
Toast blocked = Toast.makeText(context,"Blocked SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG);
blocked.show();
// Post to MySQL database
abortBroadcast();
}
for(int i=0;i<8;i++) {
System.out.println("Blocking SMS");
}
}
}
}
Basically what I’m wanting to do is post to an external PHP form from within the app when an event is fired.
I’m not sure if I understood your question.. So basicly you want to post some data to a web server when some event happens?
I see you already have the code to trigger this event so you are only asking how to post data? Here is an example: