我如何获得的手机短信发送者联系(人)保存名称中使用"内容://短信/收件箱" [英] How do i get the SMS Sender Contact (Person) saved name using "content://sms/inbox"

查看:206
本文介绍了我如何获得的手机短信发送者联系(人)保存名称中使用"内容://短信/收件箱"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想用低于code取回手机短信发送者保存的名称,但它总是返回null。请给我建议什么的方便的方式来获得发件人的名字。

In my App i want to retrieve the SMS sender saved Name using below code but it always return null. Please suggest me whats the convenient way to get the sender name.

Uri SMS_INBOX = Uri.parse("content://sms/inbox");
         Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
         android.util.Log.i("COLUMNS", Arrays.toString(c.getColumnNames()));

            try {
                if(c.getCount()>0)
                {
                    while (c.moveToNext()){
                     Log.d("SMSss", "Contact : "+c.getString(2)+"\n"
                         +"msg : "+c.getString(11)+"\n"
                         +"ID : "+c.getString(0)+"\n"
                         +"Person : "+c.getString(3));
                    }
                }
            } catch (Exception e) {

            Log.d("mmmmmmmmm"," "+ e.getStackTrace());
            }

我用下面的许可menifest

i am using following permission in menifest

 <uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

请建议我怎么走。先谢谢了。

Please suggest me how to get. Thanks in advance.

推荐答案

通过这种方式,你可以从收件箱中保存的联系人姓名..  调用该方法 getAllSms()以获得详细信息。

By this way you can get the saved contact name from inbox.. call the method getAllSms() to get the details..

 public void getAllSms() {
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(message, null, null, null, null);
    startManagingCursor(c);
    int totalSMS = c.getCount();
    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {

            Log.d("SMSss",
                    "Contact number : "
                            + c.getString(c
                                    .getColumnIndexOrThrow("address"))
                            + "\n"
                            + "msg : "
                            + c.getColumnIndexOrThrow("body")
                            + "\n"
                            + "ID : "
                            + c.getString(c.getColumnIndexOrThrow("_id"))
                            + "\n"
                            + "Person : "
                            + getContactName(
                                getApplicationContext(),
                                    c.getString(c
                                            .getColumnIndexOrThrow("address"))));

            c.moveToNext();
        }
    }
    c.close();

}

public String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri,
            new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor
                .getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return contactName;
}

这篇关于我如何获得的手机短信发送者联系(人)保存名称中使用&QUOT;内容://短信/收件箱&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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