【问题标题】:Sending SMS Message from Android App从 Android 应用程序发送 SMS 消息
【发布时间】:2014-07-22 11:28:01
【问题描述】:

我正在制作一个简单的 android 应用程序来向移动网络发送消息。我正在使用以下代码,但是当我输入电话号码和短信并单击发送按钮时,它不会发送消息并显示消息发送失败。

我的主要活动“Main.java”是这个..

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.telephony.SmsManager;
import android.widget.Toast;

public class Main extends ActionBarActivity {

    Button Btn;
    EditText textphoneNo;
    EditText Message;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        Btn = (Button) findViewById(R.id.SendSMS);
        textphoneNo = (EditText) findViewById(R.id.TextPhoneNo);
        Message = (EditText) findViewById(R.id.TextSMS);


        Btn.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
              sendSMSMessage();
           }

        protected void sendSMSMessage() {
            Log.i("Send SMS", "");

            String phoneNo = textphoneNo.getText().toString();
            String message = Message.getText().toString();

            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, message , null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent.",
                Toast.LENGTH_LONG).show();
             } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                "SMS Sending Failed.",
                Toast.LENGTH_LONG).show();
                e.printStackTrace();
             }
          }
          @SuppressWarnings("unused")
        public boolean onCreateOptionsMenu(Menu menu) {
             // Inflate the menu; this adds items to the action bar if it is present.
             getMenuInflater().inflate(R.menu.main, menu);
             return true;
        }
    });

    }
  }

当点击发送按钮时,控件进入 catch 语句并且没有发送消息,请告诉我这里的问题是什么,没有让控件进入 try 语句。

【问题讨论】:

  • 您有任何可发送的文本吗?如果是这样,您的堆栈跟踪报告了什么?
  • 谢谢大家,我的问题已经解决了,实际上我没有使用清单文件中的权限..现在我已经使用了权限并发送了短信。谢谢。 @Wolfish 请小心你的话,你不知道他们会伤害听众有多深。
  • Send SMS in android的可能重复

标签: android sms sendmessage


【解决方案1】:

您必须在您的 AndroidManifest.xml 中添加此权限

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

是吗?

【讨论】:

  • 非常感谢@Jose Montero 问题已解决。实际上我没有使用清单文件中的权限,现在我输入了权限,问题就解决了。再次感谢。
【解决方案2】:

使用Log.i("phonenumber",textphoneNo.getText().toString()) 查看 phoneNumber 值。我认为它的价值是null

【讨论】:

    【解决方案3】:

    确保你在 Manifest.xml 文件中添加了短信发送权限

    【讨论】:

      猜你喜欢
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 2013-11-02
      • 2010-10-12
      • 1970-01-01
      • 2012-12-25
      相关资源
      最近更新 更多