android 监听 系统message app 短信发送
其实是监听 发件箱数据的变化package com.example.testsms;import android.app.Activity;import android.database.ContentObserver;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;im
·
其实是监听 发件箱数据的变化
package com.example.testsms;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getContentResolver().registerContentObserver(
Uri.parse("content://sms"), true,
new SmsObserver(new Handler()));
}
private final class SmsObserver extends ContentObserver {
public SmsObserver(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {// 查询发送箱中的短信(处于正在发送状态的短信放在发送箱)
Cursor cursor = getContentResolver().query(
Uri.parse("content://sms/outbox"), null, null, null, null);
while (cursor.moveToNext()) {
StringBuilder sb = new StringBuilder();
sb.append("_id=").append(
cursor.getInt(cursor.getColumnIndex("_id")));
sb.append(",address=").append(
cursor.getString(cursor.getColumnIndex("address")));
sb.append(";body=").append(
cursor.getString(cursor.getColumnIndex("body")));
sb.append(";time=").append(
cursor.getLong(cursor.getColumnIndex("date")));
Log.i("ReceiveSendSMS", sb.toString());
}
}
}
}
更多推荐


所有评论(0)