开源项目之Android Ecclesia(短信应用)
Ecclesia 是一个 Android 的应用程序,该用户程序在手机接收到短信时会自动通过表单提交的方式将短信息发送到指定的URL。
项目如图:
源码简单移动,就直接贴出主要源码了!
ActivityEcclesia.java
private void onInit() { try { String[] projection = new String[] { People._ID, People.NAME, }; Uri mContacts = People.CONTENT_URI; // 读取联系人 Cursor managedCursor = managedQuery(mContacts, projection, // Which // columns // to // return null, // WHERE clause; which rows to return (all rows) null, // WHERE clause selection arguments (none) People.NAME + " ASC"); // Order-by clause (ascending by // name) Cursor cur = managedCursor; if (cur.moveToFirst()) { String id; String name; do { // Get the field values id = cur.getString(cur.getColumnIndex(People._ID)); name = cur.getString(cur.getColumnIndex(People.NAME)); if (name.equals(this.getString(R.string.sms_prefix)) != true) { continue; } String where = Contacts.Organizations.PERSON_ID + " == " + id + ""; // URL Address Cursor addressCursor = managedQuery( Contacts.ContactMethods.CONTENT_URI, null, where, null, null); int postalAddress = addressCursor .getColumnIndexOrThrow(Contacts.ContactMethodsColumns.DATA); address = ""; if (addressCursor.moveToFirst()) { address = addressCursor.getString(postalAddress); } addressCursor.close(); // Login Information Cursor orgCursor = managedQuery( Contacts.Organizations.CONTENT_URI, null, where, null, null); if (orgCursor.moveToFirst()) { do { if (orgCursor .getString( orgCursor .getColumnIndexOrThrow(Contacts.OrganizationColumns.COMPANY)) .equals(this .getString(R.string.key_user_id)) == true) { userId = orgCursor .getString(orgCursor .getColumnIndexOrThrow(Contacts.OrganizationColumns.TITLE)); } if (orgCursor .getString( orgCursor .getColumnIndexOrThrow(Contacts.OrganizationColumns.COMPANY)) .equals(this .getString(R.string.key_user_pass)) == true) { userPass = orgCursor .getString(orgCursor .getColumnIndexOrThrow(Contacts.OrganizationColumns.TITLE)); } } while (orgCursor.moveToNext()); } orgCursor.close(); // AndroidManifest.xml <uses-permission // android:name="android.permission.INTERNET" /> TextView tv; tv = (TextView) this.findViewById(R.id.textView); tv = new TextView(this); tv.setText("[" + this.getString(R.string.sms_prefix) + "] " + id + " " + name + " " + address + " " + userId + " " + userPass); setContentView(tv); WebView webView = new WebView(this); setContentView(webView); if (webView != null) { // js调用 // http://host/index.php <?php // printf(print_r($_REQUEST); ?> webView.setWebChromeClient(new WebChromeClient()); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSavePassword(false); webView.getSettings().setSaveFormData(false); webView.loadDataWithBaseURL(address, "<body onload=" + '"' + "javascript:document.forms[0].submit();" + '"' + "><form method=" + '"' + "POST" + '"' + " action=" + '"' + address + '"' + "><input type=" + '"' + "text" + '"' + " name=" + '"' + "user_id" + '"' + " value=" + '"' + userId + '"' + "/><input type=" + '"' + "password" + '"' + " name=" + '"' + "user_pass" + '"' + " value=" + '"' + userPass + '"' + "/><input type=" + '"' + "submit" + '"' + " value=" + '"' + "GO" + '"' + "></body>", "text/html", "utf-8", "http://www.google.com/"); webView.requestFocus(); } break; } while (cur.moveToNext()); } } catch (Exception e) { java.lang.System.out.println(e.getMessage()); } }
IntentReceiver.java
public class IntentReceiver extends BroadcastReceiver { //短信的接收广播 @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction() .equals("android.provider.Telephony.SMS_RECEIVED") == true) { SmsMessage msg[] = getIntentMessages(intent); for (int i = 0; i < msg.length; i++) { String message = msg[i].getDisplayMessageBody(); if (message != null && message.length() > 0) { if ((message.startsWith(R.string.sms_prefix + " ") == true) || (msg[i].getOriginatingAddress().equals("") != true)) { Intent broadcast = new Intent( "com.android.demo.WAKE_UP"); broadcast.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(new Intent(broadcast)); } } break; } } } private SmsMessage[] getIntentMessages(Intent intent) { SmsMessage message[] = null; Bundle bundle = intent.getExtras(); try { Object pdus[] = (Object[]) bundle.get("pdus"); message = new SmsMessage[pdus.length]; for (int n = 0; n < pdus.length; n++) { byte[] bytes = (byte[]) pdus[n]; message[n] = SmsMessage.createFromPdu(bytes); } } catch (Exception e) { java.lang.System.out.println(e.toString()); } return message; } }
是不是很简单啊!~