編輯:關於Android編程
MainActivity如下:
package cc.testsmslistener; import cc.testsmslistener.SMSContentObserver.MessageListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import android.app.Activity; /** * Demo描述: * 利用ContentObserver實現短信監聽 * * 注意權限: ** * 參考資料: * http://blog.csdn.net/peijiangping1989/article/details/7368178 * Thank you very much * */ public class MainActivity extends Activity { private TextView mTextView; //所有短信 public static final String URI = "content://sms/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { mTextView=(TextView) findViewById(R.id.textView); //注冊內容觀察者 SMSContentObserver smsContentObserver = new SMSContentObserver(new Handler(),this); this.getContentResolver().registerContentObserver (Uri.parse(URI), true, smsContentObserver); //回調 smsContentObserver.setOnReceivedMessageListener(new MessageListener() { @Override public void OnReceived(String message) { mTextView.setText(message); } }); } }
package cc.testsmslistener; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Handler; public class SMSContentObserver extends ContentObserver { //所有短信 public static final String SMS_URI_ALL = "content://sms/"; //收件箱短信 public static final String SMS_URI_INBOX = "content://sms/inbox"; //發件箱短信 public static final String SMS_URI_SEND = "content://sms/sent"; //草稿箱短信 public static final String SMS_URI_DRAFT = "content://sms/draft"; private Activity mActivity; private ListmSmsInfoList; private MessageListener mMessageListener; public SMSContentObserver(Handler handler, Activity activity) { super(handler); this.mActivity = activity; } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); Uri uri = Uri.parse(SMS_URI_INBOX); mSmsInfoList = this.getSmsInfo(uri,mActivity); mMessageListener.OnReceived(mSmsInfoList.get(0).getSmsbody()); System.out.println("Message content is:"+mSmsInfoList.get(0).getSmsbody()); System.out.println("Message info is:"+mSmsInfoList.get(0)); } /** * 注意: * 該處只用按照時間降序取出第一條即可 * 這條當然是最新收到的消息 */ private List getSmsInfo(Uri uri,Activity activity){ List smsInfoList=new ArrayList (); String[] projection = new String[] { "_id", "address", "person","body", "date", "type" }; Cursor cusor = activity.managedQuery(uri, projection, null, null,"date desc limit 1"); int nameColumn = cusor.getColumnIndex("person"); int phoneNumberColumn = cusor.getColumnIndex("address"); int smsbodyColumn = cusor.getColumnIndex("body"); int dateColumn = cusor.getColumnIndex("date"); int typeColumn = cusor.getColumnIndex("type"); if (cusor != null) { while (cusor.moveToNext()) { SmsInfo smsinfo = new SmsInfo(); smsinfo.setName(cusor.getString(nameColumn)); smsinfo.setDate(cusor.getString(dateColumn)); smsinfo.setPhoneNumber(cusor.getString(phoneNumberColumn)); smsinfo.setSmsbody(cusor.getString(smsbodyColumn)); smsinfo.setType(cusor.getString(typeColumn)); smsInfoList.add(smsinfo); } cusor.close(); } System.out.println("smsInfoList.size()="+smsInfoList.size()); return smsInfoList; } // 回調接口 public interface MessageListener { public void OnReceived(String message); } public void setOnReceivedMessageListener( MessageListener messageListener) { this.mMessageListener=messageListener; } }
package cc.testsmslistener; public class SmsInfo { //短信內容 private String smsbody; //發送短信的電話號碼 private String phoneNumber; //發送短信時間 private String date; //發送短信人的姓名 private String name; //短信類型1是接收到的,2是已發出 private String type; public String getSmsbody() { return smsbody; } public void setSmsbody(String smsbody) { this.smsbody = smsbody; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "SmsInfo [smsbody=" + smsbody + ", phoneNumber=" + phoneNumber + ", date=" + date + ", name=" + name + ", type=" + type + "]"; } }
直接上效果圖 功能特色: 1、可以設置刮開後顯示文字或圖片 2、可以統計已刮開區域所占百分比 Demo下載地址:RubberDem
什麼是路由?說簡單點就是映射頁面跳轉關系的,當然它也包含跳轉相關的一切功能。路由框架的意義Android系統已經給我們提供了api來做頁面跳轉,比如startActivi
Gesture在 ViewGroup中使用GestureDetector類可以讓我們快速的處理手勢事件,如點擊,滑動等。 使用GestureDetector分三步: 1.
MainActivity:package com.ruru.dialogproject; import android.app.Activity; import andr