此代码是否适用于自 Froyo 2.2 以来的任何设备/Android 版本:
public class SmsObserver extends ContentObserver {
private String Name;
private SharedPreferences myPrefs;
public SmsObserver(Handler handler , Context ctx) {
super(handler);
// TODO Auto-generated constructor stub
context = ctx;
initialPos = getLastMsgId();
}
private Context context;
private static int initialPos;
private static final String TAG = "SMSContentObserver";
private static final Uri uriSMS = Uri.parse("content://sms");
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
queryLastSentSMS();
}
public int getLastMsgId() {
Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, null);
cur.moveToFirst();
int lastMsgId = cur.getInt(cur.getColumnIndex("_id"));
Log.i(TAG, "Last sent message id: " + String.valueOf(lastMsgId));
return lastMsgId;
}
protected void queryLastSentSMS() {
new Thread(new Runnable() {
public void run() {
Cursor cur =
context.getContentResolver().query(uriSMS, null, null, null, null);
if (cur.moveToNext()) {
TelephonyManager tm = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
String myDeviceId = tm.getDeviceId();
String myTelephoneNumber = tm.getLine1Number();
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int seconde = c.get(Calendar.SECOND);
try {
String body = cur.getString(cur.getColumnIndex("body"));
String reformated_body =
Normalizer.normalize(body, Normalizer.Form.NFD).replaceAll(
"[^\\p{ASCII}]", "");
if (initialPos != getLastMsgId()) {
myPrefs = context.getSharedPreferences("Preferences", Context.MODE_WORLD_WRITEABLE);
getDisplayNameFromPhoneNo(cur.getString(cur.getColumnIndex("address")));
final String finalTxt = context.getResources().getString(R.string.txtSMSSent) + " " + Name + " " + cur.getString(cur.getColumnIndex("address")) + " " + context.getResources().getString(R.string.txtSMSMessage) + reformated_body;
Log.i(TAG, "Reformated Body : " + reformated_body);
Log.i("account", myDeviceId);
Log.i("date", day + "-" + month + "-" + year + " "
+ hour + ":" + minute + ":" + seconde);
Log.i("sender", myTelephoneNumber);
Log.i("receiver", cur.getString(cur.getColumnIndex("address")));
Log.i("message", reformated_body);
if(!cur.getString(cur.getColumnIndex("address")).equals(Gever)){
// Then, set initialPos to the current position.
initialPos = getLastMsgId();
}
}} catch (Exception e) {
// Treat exception here
}
}
cur.close();
}
}).start();
}
public void getDisplayNameFromPhoneNo(String phoneNo) {
ContentResolver localContentResolver = context.getContentResolver();
Cursor contactLookupCursor =
localContentResolver.query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNo)),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
try {
while(contactLookupCursor.moveToNext()){
String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
String contactId = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
Log.d("LOGTAG", "contactMatch name: " + contactName);
Log.d("LOGTAG", "contactMatch id: " + contactId);
Name = contactName;
}
} finally {
contactLookupCursor.close();
}
}
}//End of class SmsObserver
我收到一份报告说它不起作用,对我来说,它工作正常,所以这个:“content://sms”对于所有 Android 版本都一样吗?