import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
public class ObserveSMSSend extends ContentObserver{
private final String SMSSEND_TYPE="smsInfo";
private final String TYPE="send";
private static final String TAG ="SystemSens_sendsms";
private Context mcontext;
public ObserveSMSSend(Handler handler ,Context context) {
super(handler);
this.mcontext = context;
}
@Override
public void onChange( boolean selfChange) {
super.onChange(selfChange);
Cursor cursor = this.mcontext.getContentResolver().query(Uri.parse("content://sms/outbox"),null, null, null, null);
while (cursor.moveToNext()){
String address=cursor.getString(cursor.getColumnIndex("address"));
String msg=cursor.getString(cursor.getColumnIndex("body"));
Log.i("ReceiveSendSMS", address+":"+msg);
}
}
//* 在activity中添加 如下代码
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ObserveSMSSend content = new ObserveSMSSend(new Handler(), this);
this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);
}
///注意 添加 androidmanifest中添加
<uses-permission android:name="android.permission.READ_SMS"/> <uses-permission android:name="android.permission.READ_CONTACTS"/>
打开DDMS,用你的SDK 发一个短信 ,看看是systemout中有了。
那个content://sms/outbox 可以改为 content://sms/inbox 就是收件箱,患有其他,自己找去。