您的位置:首页 > 数据库

ContentObserver 监听短信数据库,收短信

2013-04-11 17:27 381 查看
ContentObserver 监听短信数据库 和 飞行模式发生改变

 

package com.example.contentobservertest;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.*;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tvAirplane;
private EditText etSmsoutbox;

// Message 类型值
private static final int MSG_AIRPLANE = 1;
private static final int MSG_OUTBOXCONTENT = 2;

private AirplaneContentObserver airplaneCO;
private SMSContentObserver smsContentObserver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvAirplane = (TextView) findViewById(R.id.tvAirplane);
etSmsoutbox = (EditText) findViewById(R.id.smsoutboxContent);

// 创建两个对象
airplaneCO = new AirplaneContentObserver(this, mHandler);
smsContentObserver = new SMSContentObserver(this, mHandler);

//注册内容观察者
registerContentObservers() ;
}

private void registerContentObservers() {
// 通过调用getUriFor 方法获得 system表里的"飞行模式"所在行的Uri
Uri airplaneUri = Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON);
// 注册内容观察者
getContentResolver().registerContentObserver(airplaneUri, false, airplaneCO);
// ”表“内容观察者 ,通过测试我发现只能监听此Uri -----> content://sms
// 监听不到其他的Uri 比如说 content://sms/outbox
Uri smsUri = Uri.parse("content://sms/inbox");
getContentResolver().registerContentObserver(smsUri, true,smsContentObserver);
}
 
 
 
 
 
 
 
package com.example.contentobservertest;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

//用来观察系统里短消息的数据库变化  ”表“内容观察者,只要信息数据库发生变化,都会触发该ContentObserver 派生类
public class SMSContentObserver extends ContentObserver {

private Context mContext  ;
private Handler mHandler ;   //更新UI线程

String[] projection = new String[]{"address",
"body", "date", "type", "read"};

public SMSContentObserver(Context context,Handler handler) {
super(handler);
mContext = context ;
mHandler = handler ;
}

@Override
public void onChange(boolean selfChange){
Log.i("xxx1", "the sms table has changed");

//查询发件箱里的内容
Uri inSMSUri = Uri.parse("content://sms/inbox") ;

Cursor c = mContext.getContentResolver().query(inSMSUri,
null,
null,
null,
"date desc");
if(c != null){

Log.i("xxx1", "the number of send is"+c.getCount()) ;

//循环遍历
while(c.moveToNext()){

SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(c.getLong(c.getColumnIndex("date")));
String date = dateFormat.format(d);
StringBuilder sb = new StringBuilder() ;
sb.append("发件人手机号码: "+c.getString(c.getColumnIndex("address")))
.append("信息内容: "+c.getString(c.getColumnIndex("body")))
.append(" 是否查看: "+c.getInt(c.getColumnIndex("read")))
.append(" 类型: "+c.getInt(c.getColumnIndex("type")))//收件箱,发件箱,草稿箱,等等
.append(date)
;
//      		      .append("\n");

Log.i("xxx", sb.toString());
}
c.close();
//        	mHandler.obtainMessage(MainActivity.MSG_OUTBOXCONTENT, sb.toString()).sendToTarget();
}
}

}


private Handler mHandler = new Handler() {public void handleMessage(Message msg) {System.out.println("---mHanlder----");switch (msg.what) {case MSG_AIRPLANE:int isAirplaneOpen = (Integer) msg.obj;if (isAirplaneOpen != 0)tvAirplane.setText("飞行模式已打开");else if
(isAirplaneOpen == 0)tvAirplane.setText("飞行模式已关闭");break;case MSG_OUTBOXCONTENT:String outbox = (String) msg.obj;etSmsoutbox.setText(outbox);break;default:break;}}};}

 
 
package com.example.contentobservertest;

import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;

//用来观察system表里飞行模式所在行是否发生变化 , “行”内容观察者
public class AirplaneContentObserver extends ContentObserver {

private static String TAG = "AirplaneContentObserver" ;

private static int MSG_AIRPLANE = 1 ;

private Context mContext;
private Handler mHandler ;  //此Handler用来更新UI线程

public AirplaneContentObserver(Context context, Handler handler) {
super(handler);
mContext = context;
mHandler = handler ;
}

/**
* 当所监听的Uri发生改变时,就会回调此方法
*
* @param selfChange 此值意义不大 一般情况下该回调值false
*/
@Override
public void onChange(boolean selfChange) {
Log.i(TAG, "-------------the airplane mode has changed-------------");

// 系统是否处于飞行模式下
try {
int isAirplaneOpen = Settings.System.getInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
Log.i(TAG, " isAirplaneOpen -----> " +isAirplaneOpen) ;
mHandler.obtainMessage(MSG_AIRPLANE,isAirplaneOpen).sendToTarget() ;
}
catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


添加读写通讯录的权限
 <uses-permission android:name="android.permission.READ_SMS"/>
 <uses-permission android:name="android.permission.WRITE_SMS"/>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息