ContentObserver 监听短信数据库,收短信
2013-04-11 17:27
381 查看
ContentObserver 监听短信数据库 和 飞行模式发生改变
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.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"/>
相关文章推荐
- 内容观察者 ContentObserver 监听短信、通话记录数据库 挂断来电
- Android使用ContentObserver监听数据库变化,截取系统通知用户接收到短信
- Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
- 使用ContentObserver监听短信数据库的变化
- 内容观察者 ContentObserver 监听短信、通话记录数据库 挂断来电
- Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
- android 使用contentobserver监听数据库内容变化
- 使用ContentObserver监听数据库变化
- Android_通过ContentObserver监听短信数据变化
- Android使用ContentObserver监听数据库变化
- ContentObserver监听发送的短信
- android 使用contentobserver监听数据库内容变化
- android ContentObserver监听系统短信和备份短信到本地
- 通过ContentObserver监听数据库某表的增加、删除、更新动作,实现listView异步单项Item的刷新
- 使用ContentObserver监听数据库变化
- 使用ContentObserver监听数据库变化
- Android使用ContentObserver监听数据库变化
- 使用ContentObserver监听数据库变化
- android的content provider之二:添加数据库属性及用ContentObserver实现数据库的监听
- Android短信监听(二)——利用ContentObserver实现短信监听