简体   繁体   English

无法在Android中使用SmsManager发送短信

[英]Can't send a SMS using SmsManager in android

I want to send a sms from my android phone but it doesn't work.

here is my code: 这是我的代码:

public class MainActivity extends Activity{

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
        public void sendMessage(View view){

            String msg,number;
            number= "***********";
            msg="This is a message";
           try{

                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(number, null, msg,null,null);
                Toast.makeText(getApplicationContext(), "SMS sent.",
                        Toast.LENGTH_LONG).show();

            }catch(Exception ex){
                Toast.makeText(getApplicationContext(),"SMS failed, please try again.",Toast.LENGTH_LONG).show();
                ex.printStackTrace();
            }
    }

I include 'uses-permission android:name="android.permission.SEND_SMS"' I run this code from my android phone but it doesn't send any message. 我包含“ uses-permission android:name =“ android.permission.SEND_SMS””,我从Android手机运行此代码,但未发送任何消息。 If there is any error how can i see those error and how to fix it. 如果有任何错误,我怎么能看到这些错误以及如何解决。 Please Help me.Thanks in advance. 请帮助我。谢谢。

使用本网站作为参考

<uses-permission android:name="android.permission.SEND_SMS" />

According to your code, the problem is that you haven't called the sendMessage method. 根据您的代码,问题在于您尚未调用sendMessage方法。

You need to have an action that occurs when you click a button. 单击按钮时,您需要执行一项操作。 Google how that is done on Android. Google在Android上是如何做到的。 Once you have that worked out, you should be able to make the button click to call the sendMessage method. 解决之后,您应该可以使按钮单击以调用sendMessage方法。

Also, you'll need to know how to view the error log. 另外,您将需要知道如何查看错误日志。 There are a number of apps that allow you to do this. 有许多应用程序可让您执行此操作。 It is important to know how to view the error log so that way if your Toast finally pops up, you'll be able to check the log to see why this happened. 知道如何查看错误日志非常重要,这样,如果您的Toast最终弹出,您就可以检查日志以了解发生这种情况的原因。

I guess you have the sendMessage() some where in the code if you trigger a button click? 我想如果您触发按钮单击,那么您在代码中的某些位置有sendMessage()吗? sendTextMessage work well. sendTextMessage运作良好。 Anyway, make sure that you've validated if number is null or bad formatted. 无论如何,请确保您已验证数字是否为null或格式错误。 This may causes the problem. 这可能会导致问题。 Make also sure that you are using the proper SMSMAnager (android.telephony.SmsManager) instead of android.telephony.gsm.SmsManager. 还要确保您使用的是正确的SMSMAnager(android.telephony.SmsManager),而不是android.telephony.gsm.SmsManager。 SEND_SMS permission is also required. 还需要SEND_SMS权限。

I solve the problem.Actually the problem is for dual sim. 我解决了问题。实际上问题出在双卡双待上。 And always i deactivate my first sim(Sim1). 而且我总是停用我的第一个sim(Sim1)。 But this is the problem.My app always find my first sim message center number but i deactivate Sim1.Now i activate my first sim and this is worked.Thanks to all for your answer and help 但这是问题所在。我的应用程序始终会找到我的第一个sim短信中心号码,但我停用了Sim1。现在我激活了我的第一个sim卡,此方法就可以了。感谢大家的回答和帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM