Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android短信監聽(二)——利用ContentObserver實現短信監聽

Android短信監聽(二)——利用ContentObserver實現短信監聽

編輯:關於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);
			}
		});
	}

}

SMSContentObserver如下:

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

SmsInfo如下:

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 + "]";
	}
	
	
}

main.xml如下:



    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved