2012-06-26 56 views
0

我正在開發一個Android應用程序,當收到傳入消息時我需要發佈到PHP表單或插入到MySQL數據庫中。有些人可能會猶豫是否要回應,但讓我向你保證,這不是一個惡意的應用程序。在傳入的短信中觸發一條信息到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窗體。

回答

0

我不知道如果我理解你的問題。所以basicly要一些數據發佈到當一些事件發生在一個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); 
+0

這似乎是可行的。當我把我的網站的網址你的例子裏面[例:新HttpPost(http://test.com); ]斜槓被誤認爲是評論。有沒有一種方法可以修改我的URL以避免這種情況,還是我必須使用IP地址? – localhost

+0

你需要引用url字符串。見編輯答案 – Caner

+0

哇。在我的匆忙中,我一定忽略了這一點。謝謝。 – localhost