增加选择联系人按钮

This commit is contained in:
zengfantian 2015-02-10 18:23:48 +08:00
parent ba8327de45
commit fdcebf617c
3 changed files with 128 additions and 36 deletions

View File

@ -9,12 +9,14 @@
tools:context=".sample.data.AutoGainFormActivity" >
<!-- 账号 -->
<LinearLayout
android:id="@+id/ll_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="18dp" >
android:layout_marginTop="18dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_phonenumber"
@ -26,20 +28,30 @@
<EditText
android:id="@+id/et_phonenumber"
style="@style/accountEditText"
android:inputType="phone"
android:layout_width="0dip"
android:layout_weight="1.0"
android:hint="@string/et_phonenumber_hint"
android:inputType="phone"
android:background="@drawable/view_linnerlayout_border"
android:tag="phonenumber" />
<Button
android:id="@+id/btn_choice"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/greenyellow"
android:text="@string/btn_choice" />
</LinearLayout>
<!-- 账号 -->
<LinearLayout
android:id="@+id/ll_content"
android:layout_below="@id/ll_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="18dp" >
android:layout_below="@id/ll_account"
android:layout_marginTop="18dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_content"
@ -52,13 +64,13 @@
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:minLines="5"
android:background="@null"
android:textColor="@color/gray"
android:textSize="14sp"
android:hint="@string/tv_content"
android:tag="smscontent" />
android:inputType="textMultiLine"
android:background="@drawable/view_linnerlayout_border"
android:minLines="5"
android:tag="smscontent"
android:textColor="@color/gray"
android:textSize="14sp" />
</LinearLayout>
<!-- 提交表单 -->
@ -77,8 +89,8 @@
android:layout_marginLeft="4dp"
android:background="@color/orange"
android:text="@string/btn_send" />
<Button
<Button
android:id="@+id/btn_bind"
style="@style/accountButtonStyle"
android:layout_marginLeft="4dp"

View File

@ -50,6 +50,7 @@
<string name="tv_content">请输入要发送的短信内容</string>
<string name="btn_send">发送短信</string>
<string name="btn_bind">绑定拦截</string>
<string name="btn_choice">选择</string>
<!-- 短信操作示例(结束) -->
<string name="btn_submit">提交</string>

View File

@ -1,7 +1,12 @@
package com.zftlive.android.sample.sms;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@ -12,12 +17,14 @@ import com.zftlive.android.tools.ToolAlert;
import com.zftlive.android.tools.ToolSMS;
import com.zftlive.android.tools.ToolString;
public class SMSOperationActivity extends BaseActivity implements View.OnClickListener{
public class SMSOperationActivity extends BaseActivity implements
View.OnClickListener {
private EditText et_phonenumber,et_content;
private Button btn_send,btn_bind;
private EditText et_phonenumber, et_content;
private Button btn_send, btn_bind, btn_choice;
private SMSBroadcastReceiver mSMSBroadcastReceiver;
private final static int CHOICE_PHONE = 1;
@Override
public int bindLayout() {
return R.layout.activity_sms_operation;
@ -25,14 +32,17 @@ public class SMSOperationActivity extends BaseActivity implements View.OnClickLi
@Override
public void initView(View view) {
et_phonenumber = (EditText)findViewById(R.id.et_phonenumber);
et_content = (EditText)findViewById(R.id.et_content);
btn_send = (Button)findViewById(R.id.btn_send);
et_phonenumber = (EditText) findViewById(R.id.et_phonenumber);
et_content = (EditText) findViewById(R.id.et_content);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(this);
btn_bind = (Button)findViewById(R.id.btn_bind);
btn_bind = (Button) findViewById(R.id.btn_bind);
btn_bind.setOnClickListener(this);
btn_choice = (Button) findViewById(R.id.btn_choice);
btn_choice.setOnClickListener(this);
}
@Override
@ -47,38 +57,47 @@ public class SMSOperationActivity extends BaseActivity implements View.OnClickLi
@Override
public void destroy() {
if(mSMSBroadcastReceiver != null && mSMSBroadcastReceiver.isOrderedBroadcast()){
//取消订阅广播
if (mSMSBroadcastReceiver != null
&& mSMSBroadcastReceiver.isOrderedBroadcast()) {
// 取消订阅广播
unregisterReceiver(mSMSBroadcastReceiver);
}
}
@Override
public void onClick(View v) {
String phoneNumber = et_phonenumber.getText().toString();
String strContent = et_content.getText().toString();
switch (v.getId()) {
case R.id.btn_choice:
// 跳转至选择联系人界面
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
this.startActivityForResult(intent, CHOICE_PHONE);
break;
case R.id.btn_bind:
if(ToolString.isNoBlankAndNoNull(phoneNumber)){
if (ToolString.isNoBlankAndNoNull(phoneNumber)) {
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
filter.setPriority(Integer.MAX_VALUE);
mSMSBroadcastReceiver = new SMSBroadcastReceiver();
registerReceiver(mSMSBroadcastReceiver, filter);
ToolAlert.showShort(this, "绑定拦截成功");
}else{
} else {
ToolAlert.showShort(this, "手机号不能为空");
}
break;
case R.id.btn_send:
if(ToolString.isNoBlankAndNoNull(phoneNumber)&&ToolString.isNoBlankAndNoNull(strContent)){
if (ToolString.isNoBlankAndNoNull(phoneNumber)
&& ToolString.isNoBlankAndNoNull(strContent)) {
ToolSMS.sendMessage(phoneNumber, strContent);
// ToolSMS.sendMessage(this, phoneNumber, strContent);//跳转到发送短信界面
}else{
// ToolSMS.sendMessage(this, phoneNumber,
// strContent);//跳转到发送短信界面
} else {
ToolAlert.showShort(this, "手机号和短信内容两者都不能为空");
}
break;
@ -87,4 +106,64 @@ public class SMSOperationActivity extends BaseActivity implements View.OnClickLi
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CHOICE_PHONE:
if (Activity.RESULT_OK == resultCode) {
Uri uri = data.getData();
Cursor c = managedQuery(uri, null, null, null, null);
c.moveToFirst();
et_phonenumber.setText(getContactPhone(c));
}
break;
default:
break;
}
}
// 获取联系人电话
private String getContactPhone(Cursor cursor) {
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = cursor.getInt(phoneColumn);
String phoneResult = "";
// System.out.print(phoneNum);
if (phoneNum > 0) {
// 获得联系人的ID号
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
// 获得联系人的电话号码的cursor;
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
// int phoneCount = phones.getCount();
// allPhoneNum = new ArrayList<String>(phoneCount);
if (phones.moveToFirst()) {
// 遍历所有的电话号码
for (; !phones.isAfterLast(); phones.moveToNext()) {
int index = phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int typeindex = phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int phone_type = phones.getInt(typeindex);
String phoneNumber = phones.getString(index);
switch (phone_type) {
case 2:
phoneResult = phoneNumber;
break;
}
// allPhoneNum.add(phoneNumber);
}
if (!phones.isClosed()) {
phones.close();
}
}
}
return phoneResult;
}
}