简体   繁体   English

如何在Android中将SMS发送到PHP页面?

[英]How to send SMS in android to a PHP page?

I am a newcomer to android programming. 我是android编程的新手。 I have made a code to read the sms in the smartphone. 我已经编写了一个代码来读取智能手机中的短信。

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

}

This code basically displays every received messages in the phone. 此代码基本上显示了手机中收到的所有消息。 How to send the output of this code to a PHP page or to an external database field. 如何将此代码的输出发送到PHP页面或外部数据库字段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM