从联系人视图,如何将联系号码发送到我的短信应用程序,并向其发送短信

问题描述 投票:0回答:1

我构建了一个短信应用程序,支持短信活动的所有功能(仅短信)。但现在,我的问题是,当我的应用程序已经是短信的默认应用程序时,当我想通过我的应用程序向它发送短信时,我无法从联系人视图中获取号码。这是解释我想要实现的目标的图像。

当我点击短信图标时,我的应用程序打开时显示“我无法获取号码” When I click to the sms icon, my app is opened by I can't get the number

我没有任何代码来处理活动中的 SEND/SENDTO 操作,但我只是在清单文件中提到了意图过滤器:action.SEND、action.SENDTO,因为如果我们想要制作应用程序,它是必须的可选择作为默认短信应用程序。我以为联系人视图中的号码是从 onActivityResult 访问的,但似乎不起作用,请帮忙!

android sms contacts
1个回答
0
投票

从联系人获取

SENDTO
时,该号码(可能是多个号码)将作为数据
Uri
附加到启动您的
Intent
Activity
上。初始化您的
Activity
时,检查是否有适当的操作,并在必要时检索号码。

举一个基本的例子:

if (Intent.ACTION_SENDTO.equals(getIntent().getAction())) {
    Uri data = getIntent().getData();
    String numbers = data.getSchemeSpecificPart();
}

为了更可靠的实现,明智的做法是删除

Uri
上可能存在的任何其他参数,并替换任何非拉丁数字。

if (Intent.ACTION_SENDTO.equals(getIntent().getAction())) {
    Uri data = getIntent().getData();
    String numbers = data.getSchemeSpecificPart();

    // Strip any extraneous parameters
    int i = numbers.indexOf('?');
    numbers = (i == -1) ? numbers : numbers.substring(0, i);

    // Replace non-Latin digits, and ensure our delimiter is something we expect
    numbers = PhoneNumberUtils.replaceUnicodeDigits(numbers).replace(",", ";");
    ...
}

如果收到多个号码,则应以逗号或分号分隔

String
。上面用分号替换了逗号,所以我们以后不需要担心使用了哪个。如果您收到了多个号码,您只需
split()
numbers
即可获取单独的号码。

© www.soinside.com 2019 - 2024. All rights reserved.