Android短信监听(二)——利用ContentObserver实现短信监听

简介: MainActivity如下: package cc.testsmslistener;import cc.testsmslistener.SMSContentObserver.

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实现短信监听
 * 
 * 注意权限:
 * <uses-permission android:name="android.permission.READ_SMS" />
 * 
 * 参考资料:
 * 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<SmsInfo> 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<SmsInfo> getSmsInfo(Uri uri,Activity activity){
		List<SmsInfo> smsInfoList=new ArrayList<SmsInfo>();
		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如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_centerInParent="true"
    />

</RelativeLayout>


相关文章
|
存储 API Android开发
Android接收短信-createFromPdu
Android接收短信-createFromPdu
156 0
Android--短信拦截及IP拨号
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/51025730 public ...
891 0