获取手机联系人---手机短信

String NAME = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;//获取联系人名称
String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;//获取联系人号码
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;//获取phone表的Uri
Uri uri = Telephony.Sms.CONTENT_URI; //获取短信的数据库表
获取发送过来的短信号码并发送短信
public class MainActivity extends Activity {
    Uri uri = Telephony.Sms.CONTENT_URI;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ContentResolver resolver = getContentResolver();
        ContentObserver observer = new ContentObserver(null) {
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                Cursor cursor = resolver.query(uri, null, null, null, null);
                cursor.moveToFirst();
                String address = cursor.getString(cursor.getColumnIndex("address"));
                int type = cursor.getInt(cursor.getColumnIndex("type"));
                if(address.endsWith("7500") && type == 1){//1表是发送过来的数据
                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(address, null, "自动发送", null, null);
                }
            }
        };
        resolver.registerContentObserver(uri, true, observer);
    }
}

你可能感兴趣的:(获取手机联系人---手机短信)