Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SMSReceiver.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.rob57530.smsserver;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLEncoder;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.StatusLine;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import android.app.ActivityManager;
  14. import android.app.ActivityManager.RunningServiceInfo;
  15. import android.content.BroadcastReceiver;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.content.SharedPreferences;
  19. import android.os.AsyncTask;
  20. import android.preference.PreferenceManager;
  21. import android.telephony.SmsMessage;
  22. import android.util.Base64;
  23. import android.util.Log;
  24. public class SMSReceiver extends BroadcastReceiver
  25. {
  26. class RequestTask extends AsyncTask<String, String, String>
  27. {
  28. @Override
  29. protected String doInBackground(String... uri)
  30. {
  31. HttpClient httpclient = new DefaultHttpClient();
  32. HttpResponse response;
  33. String responseString = null;
  34. try
  35. {
  36. HttpGet get = new HttpGet(uri[0]);
  37. get.setHeader("Host", uri[1]);
  38. get.setHeader("Authorization", "Basic " + Base64.encodeToString("__gates__:gatespasswd".getBytes(), Base64.NO_WRAP));
  39. response = httpclient.execute(get);
  40. StatusLine statusLine = response.getStatusLine();
  41. if (statusLine.getStatusCode() == HttpStatus.SC_OK)
  42. {
  43. ByteArrayOutputStream out = new ByteArrayOutputStream();
  44. response.getEntity().writeTo(out);
  45. out.close();
  46. responseString = out.toString();
  47. }
  48. else
  49. {
  50. response.getEntity().getContent().close();
  51. throw new IOException(statusLine.getReasonPhrase());
  52. }
  53. }
  54. catch (ClientProtocolException e)
  55. {
  56. e.printStackTrace();
  57. }
  58. catch (IOException e)
  59. {
  60. e.printStackTrace();
  61. }
  62. return responseString;
  63. }
  64. @Override
  65. protected void onPostExecute(String result)
  66. {
  67. super.onPostExecute(result);
  68. Log.d("SMSServerReceiver", result);
  69. }
  70. }
  71. @Override
  72. public void onReceive(Context context, Intent intent)
  73. {
  74. Log.d("SMSServerReceiver", "sms");
  75. SharedPreferences pr = PreferenceManager.getDefaultSharedPreferences(context);
  76. if (!pr.getBoolean("running", false))
  77. return;
  78. Log.d("SMSServerReceiver", "sms (running)");
  79. SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
  80. String http = p.getString("http", "");
  81. if(http.length() == 0)
  82. return;
  83. if(intent.getExtras() != null)
  84. {
  85. Object[] smsExtra = (Object[]) intent.getExtras().get("pdus");
  86. if(smsExtra != null)
  87. for(int i=0;i<smsExtra.length;i++)
  88. {
  89. try
  90. {
  91. SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
  92. String body = sms.getMessageBody().toString();
  93. String address = sms.getOriginatingAddress();
  94. RequestTask r = new RequestTask();
  95. r.execute("http://" + http + "/received.php?sender=" + URLEncoder.encode(address, "utf-8") + "&text=" + URLEncoder.encode(body, "utf-8"), http);
  96. }
  97. catch (UnsupportedEncodingException e)
  98. {
  99. e.printStackTrace();
  100. }
  101. }
  102. }
  103. }
  104. }