[英]Why is my app not able to send SMS even with requisite permissions?
My app is trying to send a sms to another number. 我的应用正在尝试向另一个号码发送短信。 I have the requisite permission(SEND_SMS) granted by the user.
我具有用户授予的必要权限(SEND_SMS)。 The message however does not get sent.The default messaging app display the message with a "failure to send/Resend" prompt.
但是,该消息不会发送。默认的消息传递应用程序显示消息,并带有“发送/重新发送失败”提示。 What is the reason behind this and how do I fix this?
这背后的原因是什么,我该如何解决? My code is pretty straightforward-
我的代码非常简单-
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneno, null, txt, null, null);
Note- My app operates well below the sms frequency limit Edit : It works fine when run from an activity but fails when run from a service. 注意-我的应用程序在短信频率限制以下运行良好。编辑:从活动运行时,它运行正常,但从服务运行时,则运行失败。 What's the reasoning behind this ?
这背后的原因是什么?
The problem seemed to be because of where the code was getting called from . 问题似乎是由于从哪里调用了代码。 I had a service (running on a seperate thread) spawn a new thread to send out the SMSs , for some reason this seemed to lead to problems.
我有一个服务(在单独的线程上运行)产生了一个新的线程来发送SMS,出于某种原因,这似乎导致了问题。 After I removed all that unnecessary complexity and have the sms sent directly from the service it works fine.
在删除所有不必要的复杂性并直接从服务发送短信后,它可以正常工作。
Use this code instead: 请改用以下代码:
Intent sInt = new Intent(Intent.ACTION_VIEW);
sInt.putExtra("address", new String[]{txtMobile.getText().toString()});
sInt.putExtra("sms_body",txtMessage.getText().toString());
sInt.setType("vnd.android-dir/mms-sms");
Make sure you have this tag in your manifest: 确保清单中有此标记:
<uses-permission android:name="android.permission.SEND_SMS"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.