android - 即使显示已发送短信,我也没有收到任何短信
问题描述
这是我从我的应用程序发送短信的代码:
protected void send() {
String no = mobileno.getText().toString();
String msg = message.getText().toString();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(no, null, msg, null, null);
Toast.makeText(getApplicationContext(), "sms sent", Toast.LENGTH_LONG).show();
}
我的activity.xml 代码是:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.spinnerex">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
它显示已发送短信,但我没有收到任何短信。它显示没有错误,但没有收到短信。
解决方案
您是否为用户实施了权限请求?您必须获得使用 SmsManager 的许可。
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},1);
但这种方式是最糟糕的。您必须直接询问用户。你可以谷歌一下。为了测试是否是问题所在,您可以在设置 -> 您的应用设置 -> 应用权限中手动设置它。
作为一个选项,您可以将此作为参考。
推荐阅读
- mongodb - 无法从 MongoDB 解码嵌入式文档(嵌套)
- r - 根据值和出现过滤相关矩阵
- javascript - 使用 Elvis (?) 运算符后,如何使 VSCode 为 .JS 文件保持正确的语法突出显示?
- java - 使用 apache qpid-client 连接到 Azure 服务总线的问题
- javascript - 我可以从 test.each 标记模板文字中为另一个上下文捕获测试吗?
- ajax - xhr 参数在哪里?
- c++ - C ++中的'this'是指向引用的指针?
- math - 将 SVG 矩阵变换值转换为 x,y 坐标
- javascript - CSS 过渡不适用于 Javascript
- sql-server - 用于链接 SAP-B1-HANA 服务器的 SSMS 中的简单 SQL 返回空白行