如何仅通过电话号码获取联系人姓名?

发布于 2024-11-26 04:18:44 字数 2388 浏览 3 评论 0原文

我正在制作一个应用程序,在其中监听所有传入的 SMS 消息并将它们安全地发送到我的服务器上的数据库。我还想发送所有收到的 SMS 消息的显示名称,实现此目的的最佳方法是什么?有没有一种方法可以对传入消息执行此操作,或者实现此目的的唯一方法是创建一个函数,在我的联系人中搜索与我从传入 SMS 消息中获得的 smsMessage[0].getOriginatingAddress() 相同的号码。这是我找到的函数和传入消息的代码:

public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    // show first message
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.qas.im/web/add_sms.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("from", smsMessage[0].getOriginatingAddress()));
        nameValuePairs.add(new BasicNameValuePair("msg", smsMessage[0].getMessageBody()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        httpclient.execute(httppost);
    } catch (ClientProtocolException e) {} catch (IOException e) {}
    Toast toast = Toast.makeText(context, "Sent to Server \n\n" + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
    toast.show();
}

public String getContactName(final String phoneNumber) 
{  
    Uri uri;
    String[] projection;

    if (Build.VERSION.SDK_INT >= 5)
    {
        uri = Uri.parse("content://com.android.contacts/phone_lookup");
        projection = new String[] { "display_name" };
    }
    else
    { 
        uri = Uri.parse("content://contacts/phones/filter");
        projection = new String[] { "name" }; 
    } 

    uri = Uri.withAppendedPath(uri, Uri.encode(phoneNumber)); 
    Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

    String contactName = "";

    if (cursor.moveToFirst()) 
    { 
        contactName = cursor.getString(0);
    } 

    cursor.close();
    cursor = null;

    return contactName; 
}

它工作得很好,但 getContactName() 有一个错误:

The method getContentResolver() is undefined for the type SMSReceiver

可能是什么问题?非常感谢任何帮助。

I am making an application where I listen to all incoming SMS messages and send them securely to a database on my server. I want to also send the display names of all my incoming SMS messages, what is the best way to accomplish this? IS there a way I can do it with incoming messages or is the only way to accomplish this is to make a function that will search my contacts for the same number as the smsMessage[0].getOriginatingAddress() I get from the incoming SMS messages. Here is my function I found and my code for incoming messages:

public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    // show first message
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.qas.im/web/add_sms.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("from", smsMessage[0].getOriginatingAddress()));
        nameValuePairs.add(new BasicNameValuePair("msg", smsMessage[0].getMessageBody()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        httpclient.execute(httppost);
    } catch (ClientProtocolException e) {} catch (IOException e) {}
    Toast toast = Toast.makeText(context, "Sent to Server \n\n" + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
    toast.show();
}

public String getContactName(final String phoneNumber) 
{  
    Uri uri;
    String[] projection;

    if (Build.VERSION.SDK_INT >= 5)
    {
        uri = Uri.parse("content://com.android.contacts/phone_lookup");
        projection = new String[] { "display_name" };
    }
    else
    { 
        uri = Uri.parse("content://contacts/phones/filter");
        projection = new String[] { "name" }; 
    } 

    uri = Uri.withAppendedPath(uri, Uri.encode(phoneNumber)); 
    Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

    String contactName = "";

    if (cursor.moveToFirst()) 
    { 
        contactName = cursor.getString(0);
    } 

    cursor.close();
    cursor = null;

    return contactName; 
}

It works just fine, but the getContactName() has one error:

The method getContentResolver() is undefined for the type SMSReceiver

What could be the problem? Any help is really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2024-12-03 04:18:44

我认为问题可能是BroadcastReceiver不继承自Context。当您获取 contentresolver 时,您需要使用传递到 onReceive() 中的 Context。因此,在 getContactName() 方法中,

Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

您应该使用以下内容而不是:

Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); 

I think the problem might be that BroadcastReceiver does not inherit from Context. You need to use the Context that is passed into onReceive() when you are getting the contentresolver. So, in the getContactName() method, instead of this:

Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

you should use this:

Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文