名不显示上的检索短信的详细信息? [英] Names not displayed on Retrieving SMS details?

查看:124
本文介绍了名不显示上的检索短信的详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用2.1平台,我可以检索通过使用下面的code.But只显示号码和短信身体,名称还没来短信的细节,我能做些什么也检索名称?

I am using 2.1 platform I can retrieve the SMS details by using the below code.But the number and the SMS Body is only displayed, Name hasn't come yet, what can I do to retrieve the Name also ?

code

public class SMSActivity extends Activity {

    ListView lview;
    String Body  = "" ; 
    ArrayList<String> smslist=new ArrayList<String>();
    ArrayAdapter<String> itemAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);
    lview =(ListView)findViewById(R.id.lv);
    itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,smslist);
    lview.setAdapter(itemAdapter);

    ContentResolver cr = getContentResolver();
    Cursor c = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
    while(c.moveToNext()){

    Number = c.getString(c.getColumnIndexOrThrow("address"));

   Body = c.getString(c.getColumnIndexOrThrow("body")).toString();

   smslist.add( Number + ":" +"\n"+ Body);
    }
    itemAdapter.notifyDataSetChanged();

    c.close();
}
} 

如何解决这个问题呢?

How to solve this problem ?

推荐答案

您的编号右!现在这个数字传递到该方法将返回与您联系相应数量的名称。

You have Number right!! now pass this number into this method it will return you contact name of corresponding number.

   public String getContactName(String number) {
        String cName = null;
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
        String nameColumn[] = new String[]{PhoneLookup.DISPLAY_NAME};
        Cursor c =  getContentResolver().query(uri, nameColumn, null, null, null);
        if(c == null || c.getCount() == 0)
            return cName;
        c.moveToFirst();
        cName = c.getString(0);
        return cName;

    }

下面添加权限

&LT;使用许可权的android:NAME =android.permission.READ_CONTACTS/&GT;

在您的清单文件中Read_contact数据。

in your manifest file to Read_contact data.

您code替换此code

for your code replace this code

Number = c.getString(c.getColumnIndexOrThrow("address"));

   Body = c.getString(c.getColumnIndexOrThrow("body")).toString();

   smslist.add( Number + ":" +"\n"+ Body);

下面

   Number = c.getString(c.getColumnIndexOrThrow("address"));
   String name = getContactName(Number); // declare name outside

   Body = c.getString(c.getColumnIndexOrThrow("body")).toString();
  if( name == null )
     smslist.add( Number + ":" +"\n"+ Body);
  else
        smslist.add( name + ":" +"\n"+ Body);

这篇关于名不显示上的检索短信的详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆