5.AutoCompleteTextView、自定义广播

新建信息

布局:自动出来的是系统的组件,里面是listview,写ontextchanglis也行
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:padding="5dp"
  5. android:orientation="vertical" >
  6. 。。。。。
  7. <!-- android:completionThreshold="1" 当弹出建议框的时候,actv中输入的字符的个数 -->
  8. <AutoCompleteTextView
  9. android:layout_width="0dp"
  10. android:layout_weight="2"
  11. android:layout_height="wrap_content"
  12. android:id="@+id/actv"
  13. android:textColor="@android:color/black"
  14. android:completionThreshold="1"
  15. android:background="@drawable/et_common_bg"
  16. />
  17. <ImageView
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:background="@drawable/select_contact_bg"
  21. android:id="@+id/iv_select_contact"
  22. />
  23. </LinearLayout>
  24. <EditText
  25. android:layout_width="match_parent"
  26. android:layout_height="0dp"
  27. android:layout_weight="2"
  28. android:background="@drawable/et_common_bg"
  29. android:id="@+id/et_input_msg"
  30. android:gravity="top"//写上top默认就在上面
  31. />
  32. <Button
  33. android:id="@+id/btn_send_msg"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_gravity="center_horizontal"
  37. android:background="@drawable/btn_common_bg"
  38. android:paddingLeft="10dp"
  39. android:paddingRight="10dp"
  40. android:text="发送短信" />
  41. </LinearLayout>
代码
  1. public class NewMessageUI extends Activity implements OnClickListener {
  2. private AutoCompleteTextView actv;
  3. private ImageView selectContact;
  4. private EditText inputMsg;
  5. protected void onCreate(android.os.Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_new_message);
  8. actv = (AutoCompleteTextView) findViewById(R.id.actv);
  9. selectContact = (ImageView) findViewById(R.id.iv_select_contact);
  10. inputMsg = (EditText) findViewById(R.id.et_input_msg);
  11. selectContact.setOnClickListener(this);
  12. findViewById(R.id.btn_send_msg).setOnClickListener(this);
  13. adapter = new ACTVAdapter(this, null);
  14. actv.setAdapter(adapter);
  15. adapter.setFilterQueryProvider(new FilterQueryProvider() {
  16. @Override
  17. /**
  18. * 当actv 中的内容发生改变时,回调此方法
  19. * @param constraint
  20. * @return
  21. */
  22. public Cursor runQuery(CharSequence constraint) {
  23. System.out.println(constraint);
  24. Cursor cursor = getContentResolver().query(MyConstants.URI_CONTACTS, projection, "data1 like '%"+constraint+"%'", null, null);
  25. return cursor;
  26. }
  27. });
  28. }
  29. /**
  30. * 要查询的列
  31. */
  32. private String[] projection = {
  33. "_id","data1","display_name"
  34. };
  35. private final int INDEX_NAME=2;
  36. private final int INDEX_NUMBER=1;
  37. private ACTVAdapter adapter;
  38. class ACTVAdapter extends CursorAdapter{
  39. public ACTVAdapter(Context context, Cursor c) {
  40. super(context, c);
  41. }
  42. @Override
  43. /**
  44. * 该方法在点击actv中弹出的listView时,将返回值设置给 actv
  45. */
  46. public CharSequence convertToString(Cursor cursor) {
  47. return cursor.getString(INDEX_NUMBER);
  48. }
  49. @Override
  50. public View newView(Context context, Cursor cursor, ViewGroup parent) {
  51. View view = View.inflate(context, R.layout.list_item_actv, null);
  52. return view;
  53. }
  54. @Override
  55. public void bindView(View view, Context context, Cursor cursor) {
  56. TextView name = (TextView) view.findViewById(R.id.tv_name_actv);
  57. TextView number = (TextView) view.findViewById(R.id.tv_number_actv);
  58. name.setText(cursor.getString(INDEX_NAME));
  59. number.setText(cursor.getString(INDEX_NUMBER));
  60. }
  61. }
  62. @Override
  63. public void onClick(View v) {
  64. switch (v.getId()) {
  65. case R.id.btn_send_msg:// 发送短信的按钮
  66. String address = actv.getText().toString();
  67. if(TextUtils.isEmpty(address.trim())){
  68. Toast.makeText(this, "请输入收件人号码", 0).show();
  69. return ;
  70. }
  71. String msg = inputMsg.getText().toString();
  72. if(TextUtils.isEmpty(msg.trim())){
  73. Toast.makeText(this, "请输入短信内容", 0).show();
  74. return ;
  75. }
  76. // 开始发送短信
  77. Tools.sendMessage(this, msg, address);
  78. // 清空输入框
  79. inputMsg.setText("");
  80. break;
  81. case R.id.iv_select_contact:// 点击选择联系人的图片
  82. Intent intent = new Intent();
  83. intent.setAction("android.intent.action.PICK");//不能显示启动,因为有可能系统程序的类名改变了
  84. intent.setData(Uri.parse("content://com.android.contacts/contacts"));
  85. // startActivity(intent);
  86. startActivityForResult(intent, 99);
  87. break;
  88. default:
  89. break;
  90. }
  91. };
  92. @Override
  93. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  94. String stringExtra = data.getStringExtra("phone");
  95. System.out.println("stringExtra::"+stringExtra);
  96. Uri uri = data.getData();
  97. Cursor cursor = getContentResolver().query(uri, new String[]{"_id"}, null, null, null);
  98. // Tools.printCursor(cursor);
  99. cursor.moveToNext();// 返回的cursor默认指向-1行 ,
  100. int contactId = cursor.getInt(0); // 仅查询一列,所以列的索引值 输入 0
  101. Cursor cursor2 = getContentResolver().query(MyConstants.URI_CONTACTS, new String[]{"data1"}, " contact_id = "+contactId, null, null);
  102. cursor2.moveToNext();// 返回的cursor默认指向-1行 ,
  103. String number = cursor2.getString(0);
  104. actv.setText(number);
  105. }
  106. }
Tools 添加方法
  1. /**
  2. * 发送短信
  3. * @param ctx
  4. * @param msg 短信内容
  5. * @param address 收信人的电话号码
  6. */
  7. public static void sendMessage(Context ctx, String msg, String address) {
  8. SmsManager smsManager = SmsManager.getDefault();
  9. //对短信内容进行切割,防止,内容过长。
  10. ArrayList<String> msgList = smsManager.divideMessage(msg);
  11. //用于启动广播的意图
  12. Intent intent = new Intent("com.itheima.smsmanager39.receive.SmsSendSuccessReceiver");
  13. PendingIntent sentIntent = PendingIntent.getBroadcast(ctx, 88, intent, PendingIntent.FLAG_ONE_SHOT);
  14. for (int i = 0; i < msgList.size(); i++) {
  15. String oneMsg = msgList.get(i);
  16. smsManager.sendTextMessage(
  17. address, // 收信人号码
  18. null, // 短信服务中心的号码
  19. oneMsg, // 要发送的内容
  20. sentIntent, // 我们发送成功后的,隐式意图
  21. null); // 对方接收成功后的,隐式意图
  22. }
  23. insertMsg2msmDb(ctx,msg,address);
  24. }
  25. /**
  26. * 将短信内容插入系统数据库
  27. * @param ctx
  28. * @param msg 短信内容
  29. * @param address 收短信的号码
  30. */
  31. private static void insertMsg2msmDb(Context ctx, String msg, String address) {
  32. ContentValues values = new ContentValues();
  33. values.put("address", address);
  34. values.put("body", msg);
  35. values.put("type", MyConstants.TYPE_SEND);
  36. ctx.getContentResolver().insert(MyConstants.URI_SMS, values);
  37. }
自定义广播,需要清单文件写action
  1. public class SmsSendSuccessReceiver extends BroadcastReceiver{
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. Toast.makeText(context, "短信已经发送成功", 0).show();
  5. }
  6. }
清单文件:注意输入的模式就变了,发送按钮在输入法上面了
  1. <activity
  2. android:name="com.itheima.smsmanager39.NewMessageUI"
  3. android:label="@string/app_name"
  4. android:windowSoftInputMode="adjustResize">
  5. </activity>





posted @ 2015-11-25 15:46  梦和远方  阅读(192)  评论(0编辑  收藏  举报