0

我正在开发一个 Android 应用程序,当收到传入消息时,我需要发布到 PHP 表单或插入 MySQL 数据库。你们中的一些人可能会犹豫是否要回复,但我向你们保证,这不是恶意应用程序。

我在下面发布的代码是我用来阻止传入消息的代码,除了一个号码。当短信被阻止时,任何人都可以帮助我找出一种方法来触发向我的服务器发帖吗?

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");
                        }

        }

}

}

基本上我想要做的是在触发事件时从应用程序内部发布到外部 PHP 表单。

4

1 回答 1

0

我不确定我是否理解你的问题。所以基本上你想在某些事件发生时将一些数据发布到 Web 服务器?

我看到您已经有了触发此事件的代码,所以您只问如何发布数据?这是一个例子:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("www.test.com");  
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
nameValuePairs.add(new BasicNameValuePair("name1", "value1"));  
nameValuePairs.add(new BasicNameValuePair("name2", "value2")); 
nameValuePairs.add(new BasicNameValuePair("name3", "value3"));   
// etc...
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpClient.execute(httpPost);
于 2012-06-26T16:48:58.073 回答