获取发件人的联系人姓名而不是电话号码
Posted
技术标签:
【中文标题】获取发件人的联系人姓名而不是电话号码【英文标题】:Get Contact name of sender instead of phone number 【发布时间】:2014-05-29 09:03:57 【问题描述】:我正在使用以下代码将 get Messages
从 Native Application
转到我的应用程序。
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
listViewSMS=(ListView)findViewById(R.id.lvSMS);
send = (Button)findViewById(R.id.btnproperty);
send.setOnClickListener(this);
textViewSMSSender=(TextView)findViewById(R.id.tvSMSSend);
textViewSMSBody=(TextView)findViewById(R.id.tvSMSBody);
cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
smsListAdapter = new SMSListAdapter(this,getModel());
listViewSMS.setAdapter(smsListAdapter);
listViewSMS.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
TextView label = (TextView) v.getTag(R.id.tvSMSSend);
CheckBox checkbox = (CheckBox) v.getTag(R.id.cbSelect);
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
private String isCheckedOrNot(CheckBox checkbox)
if(checkbox.isChecked())
return "is checked";
else
return "is not checked";
private List<SMSListModel> getModel()
if(cursor.getCount()>0)
for(i=0;i<cursor.getCount();i++)
if(cursor.moveToPosition(i))
list.add(new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body"))));
return list;
@Override
public void onClick(View v)
if( v == send)
mDialog();
public void mDialog()
// Show The Dialog with Selected SMS
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Message App");
dialog.setIcon(android.R.drawable.ic_dialog_info);
dialog.setMessage("Count : ");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
String phoneNo = "111";
if(list.size()>0)
for(i=0;i<list.size();i++)
if(list.get(i).isSelected())
try
SmsManager smsManager = SmsManager.getDefault();
StringBuilder builder = new StringBuilder();
for(SMSListModel p: list)
builder.append(p.toString());
builder.append('\n');
String sms = builder.toString();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();
catch (Exception e)
Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
dialog.dismiss();
);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
Toast.makeText(getApplicationContext(), "SMS not Sent",Toast.LENGTH_LONG).show();
dialog.dismiss();
);
dialog.show();
短信列表适配器
public class SMSListAdapter extends ArrayAdapter<SMSListModel>
private final List<SMSListModel> list;
private final Activity mContext;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public SMSListAdapter(Activity context,List<SMSListModel> list)
super(context, R.layout.listview_each_item, list);
mContext = context;
this.list = list;
static class ViewHolder
protected TextView textAddress;
protected TextView textBody;
protected CheckBox checkbox;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder viewHolder = null;
if (convertView == null)
LayoutInflater inflator = mContext.getLayoutInflater();
convertView = inflator.inflate(R.layout.listview_each_item, null);
viewHolder = new ViewHolder();
viewHolder.textAddress = (TextView) convertView.findViewById(R.id.tvSMSSend);
viewHolder.textBody = (TextView) convertView.findViewById(R.id.tvSMSBody);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.cbSelect);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
);
convertView.setTag(viewHolder);
convertView.setTag(R.id.tvSMSSend, viewHolder.textAddress);
convertView.setTag(R.id.tvSMSBody, viewHolder.textBody);
convertView.setTag(R.id.cbSelect, viewHolder.checkbox);
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.textAddress.setText(list.get(position).getAddress());
viewHolder.textBody.setText(list.get(position).getBody());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
我从地址字段中获取此人的地址,但如果存储在我的联系人中,我还想获取发件人的联系人姓名。
【问题讨论】:
【参考方案1】:这是从联系人号码中获取联系人姓名的解决方案。 只是通过没有。到下面的函数,它会返回你想要联系的名字。
tv_name.setText(getContactDisplayNameByNumber(incomingNumber));
public String getContactDisplayNameByNumber(String number)
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "Incoming call from";
ContentResolver contentResolver = c.getContentResolver();
Cursor contactLookup = contentResolver.query(uri, null, null, null, null);
try
if (contactLookup != null && contactLookup.getCount() > 0)
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
// this.id =
// contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.CONTACT_ID));
// String contactId =
// contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
finally
if (contactLookup != null)
contactLookup.close();
return name;
【讨论】:
检查我的编辑以获取完整的代码,如果可以帮助我以上是关于获取发件人的联系人姓名而不是电话号码的主要内容,如果未能解决你的问题,请参考以下文章
Android通过电话号码有效地获取联系人姓名以在ListView中显示