欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-02-13 23:08:34
...

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>