2014-11-17 68 views
0

我是android編程的新手。我已經編寫了一個代碼來讀取智能手機中的短信。如何在Android中發送短信到PHP頁面?

import java.util.Date; 

import android.app.Activity; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      textView = (TextView) findViewById(R.id.textview); 
      getSMSDetails(); 

    } 

    private void getSMSDetails() { 
    StringBuffer stringBuffer = new StringBuffer(); 
    stringBuffer.append("*********SMS History*************** :"); 
    Uri uri = Uri.parse("content://sms"); 
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 

    if (cursor.moveToFirst()) { 
     for (int i = 0; i < cursor.getCount(); i++) { 
     String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 
     String number = cursor.getString(cursor.getColumnIndexOrThrow("address")) 
            .toString(); 
     String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString(); 
     Date smsDayTime = new Date(Long.valueOf(date)); 
     String type = cursor.getString(cursor.getColumnIndexOrThrow("type")).toString(); 
     String typeOfSMS = null; 
         switch (Integer.parseInt(type)) { 
         case 1: 
           typeOfSMS = "INBOX"; 
           break; 

         case 2: 
           typeOfSMS = "SENT"; 
           break; 

         case 3: 
           typeOfSMS = "DRAFT"; 
           break; 
         } 

      stringBuffer.append("\nPhone Number:--- " + number + " \nMessage Type:--- " 
            + typeOfSMS + " \nMessage Date:--- " + smsDayTime 
            + " \nMessage Body:--- " + body); 
         stringBuffer.append("\n----------------------------------"); 
         cursor.moveToNext(); 
       } 
       textView.setText(stringBuffer); 
      } 
      cursor.close(); 
    } 

} 

此代碼基本上顯示每個收到的消息在手機中。如何將此代碼的輸出發送到PHP頁面或外部數據庫字段。

+0

使用Web服務調用和發送輸出在Web服務請求 – John

+0

你很多嘗試GCM服務 – KOTIOS

+0

使一個HTTP請求到 – raj

回答

0
public static boolean sendStringData(String url, String data) { 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000); 

      HttpPost post = new HttpPost(url); 
      StringEntity se = new StringEntity("&data=" + data); 
      post.addHeader("content-type", "application/x-www-form-urlencoded"); 
      post.setEntity(se); 

      HttpResponse response; 
      response = client.execute(post); 
      String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 
      if (resFromServer == TRUE_SERVER_RESPONSE) 
       return true; 
      else 
       return false; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return false; 
     } 
    } 
+0

如何使用gcm服務將短信發送到頁面? –