当前位置:   article > 正文

内容观察者(ContentObserver)的使用~_contentresolver.registercontentobserver参数

contentresolver.registercontentobserver参数
public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取ContentResolver
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://sms/");
// 注册ContentObserver
resolver.registerContentObserver(uri, true, new MyObserver(new Handler()));
}


private class MyObserver extends ContentObserver {


public MyObserver(Handler handler) {
super(handler);
}


// 如果观察到系统短信变化 则获取变化的最后一条短信 输出
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Toast.makeText(MainActivity.this, "短信数据库改变了~", 1).show();


ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://sms/");


// 查询改变的短信的信息
Cursor cursor = resolver.query(uri, new String[] { "address", "type", "date", "body" }, null, null, null);
cursor.moveToFirst();
String address = cursor.getString(0);
int type = cursor.getInt(1);
long date = cursor.getLong(2);
String body = cursor.getString(3);
System.out.println(address + "   " + body);
cursor.close();
}


}


}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/223503
推荐阅读
相关标签
  

闽ICP备14008679号