5.AutoCompleteTextView、自定义广播
新建信息
布局:自动出来的是系统的组件,里面是listview,写ontextchanglis也行
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical" >
。。。。。
<!-- android:completionThreshold="1" 当弹出建议框的时候,actv中输入的字符的个数 -->
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:id="@+id/actv"
android:textColor="@android:color/black"
android:completionThreshold="1"
android:background="@drawable/et_common_bg"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select_contact_bg"
android:id="@+id/iv_select_contact"
/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@drawable/et_common_bg"
android:id="@+id/et_input_msg"
android:gravity="top"//写上top默认就在上面
/>
<Button
android:id="@+id/btn_send_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/btn_common_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="发送短信" />
</LinearLayout>
代码
public class NewMessageUI extends Activity implements OnClickListener {
private AutoCompleteTextView actv;
private ImageView selectContact;
private EditText inputMsg;
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_message);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
selectContact = (ImageView) findViewById(R.id.iv_select_contact);
inputMsg = (EditText) findViewById(R.id.et_input_msg);
selectContact.setOnClickListener(this);
findViewById(R.id.btn_send_msg).setOnClickListener(this);
adapter = new ACTVAdapter(this, null);
actv.setAdapter(adapter);
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
/**
* 当actv 中的内容发生改变时,回调此方法
* @param constraint
* @return
*/
public Cursor runQuery(CharSequence constraint) {
System.out.println(constraint);
Cursor cursor = getContentResolver().query(MyConstants.URI_CONTACTS, projection, "data1 like '%"+constraint+"%'", null, null);
return cursor;
}
});
}
/**
* 要查询的列
*/
private String[] projection = {
"_id","data1","display_name"
};
private final int INDEX_NAME=2;
private final int INDEX_NUMBER=1;
private ACTVAdapter adapter;
class ACTVAdapter extends CursorAdapter{
public ACTVAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
/**
* 该方法在点击actv中弹出的listView时,将返回值设置给 actv
*/
public CharSequence convertToString(Cursor cursor) {
return cursor.getString(INDEX_NUMBER);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = View.inflate(context, R.layout.list_item_actv, null);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.tv_name_actv);
TextView number = (TextView) view.findViewById(R.id.tv_number_actv);
name.setText(cursor.getString(INDEX_NAME));
number.setText(cursor.getString(INDEX_NUMBER));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_send_msg:// 发送短信的按钮
String address = actv.getText().toString();
if(TextUtils.isEmpty(address.trim())){
Toast.makeText(this, "请输入收件人号码", 0).show();
return ;
}
String msg = inputMsg.getText().toString();
if(TextUtils.isEmpty(msg.trim())){
Toast.makeText(this, "请输入短信内容", 0).show();
return ;
}
// 开始发送短信
Tools.sendMessage(this, msg, address);
// 清空输入框
inputMsg.setText("");
break;
case R.id.iv_select_contact:// 点击选择联系人的图片
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");//不能显示启动,因为有可能系统程序的类名改变了
intent.setData(Uri.parse("content://com.android.contacts/contacts"));
// startActivity(intent);
startActivityForResult(intent, 99);
break;
default:
break;
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String stringExtra = data.getStringExtra("phone");
System.out.println("stringExtra::"+stringExtra);
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, new String[]{"_id"}, null, null, null);
// Tools.printCursor(cursor);
cursor.moveToNext();// 返回的cursor默认指向-1行 ,
int contactId = cursor.getInt(0); // 仅查询一列,所以列的索引值 输入 0
Cursor cursor2 = getContentResolver().query(MyConstants.URI_CONTACTS, new String[]{"data1"}, " contact_id = "+contactId, null, null);
cursor2.moveToNext();// 返回的cursor默认指向-1行 ,
String number = cursor2.getString(0);
actv.setText(number);
}
}
Tools 添加方法
/**
* 发送短信
* @param ctx
* @param msg 短信内容
* @param address 收信人的电话号码
*/
public static void sendMessage(Context ctx, String msg, String address) {
SmsManager smsManager = SmsManager.getDefault();
//对短信内容进行切割,防止,内容过长。
ArrayList<String> msgList = smsManager.divideMessage(msg);
//用于启动广播的意图
Intent intent = new Intent("com.itheima.smsmanager39.receive.SmsSendSuccessReceiver");
PendingIntent sentIntent = PendingIntent.getBroadcast(ctx, 88, intent, PendingIntent.FLAG_ONE_SHOT);
for (int i = 0; i < msgList.size(); i++) {
String oneMsg = msgList.get(i);
smsManager.sendTextMessage(
address, // 收信人号码
null, // 短信服务中心的号码
oneMsg, // 要发送的内容
sentIntent, // 我们发送成功后的,隐式意图
null); // 对方接收成功后的,隐式意图
}
insertMsg2msmDb(ctx,msg,address);
}
/**
* 将短信内容插入系统数据库
* @param ctx
* @param msg 短信内容
* @param address 收短信的号码
*/
private static void insertMsg2msmDb(Context ctx, String msg, String address) {
ContentValues values = new ContentValues();
values.put("address", address);
values.put("body", msg);
values.put("type", MyConstants.TYPE_SEND);
ctx.getContentResolver().insert(MyConstants.URI_SMS, values);
}
自定义广播,需要清单文件写action
public class SmsSendSuccessReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "短信已经发送成功", 0).show();
}
}
清单文件:注意输入的模式就变了,发送按钮在输入法上面了
<activity
android:name="com.itheima.smsmanager39.NewMessageUI"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize">
</activity>