You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Receiver.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Receiver 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. boolean running = false;
  76. ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  77. for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
  78. if (SMSServerService.class.getName().equals(service.service.getClassName()))
  79. {
  80. running = true;
  81. break;
  82. }
  83. if(!running)
  84. return;
  85. Log.d("SMSServerReceiver", "sms (running)");
  86. SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
  87. String http = p.getString("http", "");
  88. if(http.length() == 0)
  89. return;
  90. if(intent.getExtras() != null)
  91. {
  92. Object[] smsExtra = (Object[]) intent.getExtras().get("pdus");
  93. if(smsExtra != null)
  94. for(int i=0;i<smsExtra.length;i++)
  95. {
  96. try
  97. {
  98. SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
  99. String body = sms.getMessageBody().toString();
  100. String address = sms.getOriginatingAddress();
  101. RequestTask r = new RequestTask();
  102. r.execute("http://" + http + "/received.php?sender=" + URLEncoder.encode(address, "utf-8") + "&text=" + URLEncoder.encode(body, "utf-8"), http);
  103. }
  104. catch (UnsupportedEncodingException e)
  105. {
  106. e.printStackTrace();
  107. }
  108. }
  109. }
  110. }
  111. }