【发布时间】: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