sendTextMessage(phoneNumber的,空,消息,NULL,NULL);总是返回成功,即使消息未发送 [英] sendTextMessage(phoneNumber, null, message, null, null); always returns success even when message not sent
本文介绍了sendTextMessage(phoneNumber的,空,消息,NULL,NULL);总是返回成功,即使消息未发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用此链接 但问题是,它总是给消息发送 如何知道如果消息确实发送!
下面是code
尝试{
SmsManager的SmsManager的= SmsManager.getDefault();
smsManager.sendTextMessage(PHONENO,空,短信,NULL,NULL);
Toast.makeText(getApplicationContext(),短信已发送!,
Toast.LENGTH_LONG).show();
// ...
}
解决方案
试试这个:
添加这里面的manifest.xml:
<使用-权限的Android:名称=android.permission.SEND_SMS>
main.xml中:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=输入收件人的电话号码
/>
<的EditText
机器人:ID =@ + ID / txtPhoneNo
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
/>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=消息
/>
<的EditText
机器人:ID =@ + ID / txtMessage
机器人:layout_width =FILL_PARENT
机器人:layout_height =150像素
机器人:重力=顶
/>
<按钮
机器人:ID =@ + ID / btnSendSMS
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文=发送短信
/>
< / LinearLayout中>
MainActivity:
公共类短信扩展活动
{
按钮btnSendSMS;
的EditText txtPhoneNo;
的EditText txtMessage;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
btnSendSMS =(按钮)findViewById(R.id.btnSendSMS);
txtPhoneNo =(EditText上)findViewById(R.id.txtPhoneNo);
txtMessage =(EditText上)findViewById(R.id.txtMessage);
btnSendSMS.setOnClickListener(新查看.OnClickListener()
{
公共无效的onClick(视图v)
{
字符串PHONENO = txtPhoneNo.getText()的toString()。
字符串消息= txtMessage.getText()的toString()。
如果(phoneNo.length()大于0&安培;&安培; message.length()大于0)
sendSMS(PHONENO,消息);
其他
Toast.makeText(getBaseContext(),
请同时输入电话号码和信息。
Toast.LENGTH_SHORT).show();
}
});
}
}
sendSMS()函数:
公共类短信扩展活动
{
// ...
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState)
{
// ...
}
// ---发送SMS消息到另一装置---
私人无效sendSMS(字符串phoneNumber的,字符串消息)
{
PendingIntent PI = PendingIntent.getActivity(此,0,
新的意图(这一点,SMS.class),0);
SmsManager的短信= SmsManager.getDefault();
sms.sendTextMessage(phoneNumber的,空,消息,PI,NULL);
}
}
// ---发送SMS消息给另一个设备---
私人无效sendSMS(字符串phoneNumber的,字符串消息)
{
字符串SENT =SMS_SENT;
字符串DELIVERED =SMS_DELIVERED;
PendingIntent sentPI = PendingIntent.getBroadcast(此,0,
新的意图(SENT),0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,
新的意图(交付),0);
// ---当短信已发送---
registerReceiver(新BroadcastReceiver的(){
@覆盖
公共无效的onReceive(背景为arg0,意图ARG1){
开关(的getResult code())
{
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),短信发送
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),一般故障,
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),无服务,
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),空的PDU,
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),无线电关
Toast.LENGTH_SHORT).show();
打破;
}
}
},新的IntentFilter(发送));
// ---当SMS已交付---
registerReceiver(新BroadcastReceiver的(){
@覆盖
公共无效的onReceive(背景为arg0,意图ARG1){
开关(的getResult code())
{
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS递送,
Toast.LENGTH_SHORT).show();
打破;
案例Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(),短信未交付,
Toast.LENGTH_SHORT).show();
打破;
}
}
},新的IntentFilter(交付));
SmsManager的短信= SmsManager.getDefault();
sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
}
code来源: http://mobiforge.com/developing/story/短信消息,安卓
I tried sms activity using this link but the problem is that it always gives "message sent" how to know if the message is really sent!
here is code
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
//...
}
解决方案
Try this:
Add this inside manifest.xml:
<uses-permission android:name="android.permission.SEND_SMS">
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the phone number of recipient"
/>
<EditText
android:id="@+id/txtPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Message"
/>
<EditText
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="150px"
android:gravity="top"
/>
<Button
android:id="@+id/btnSendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send SMS"
/>
</LinearLayout>
MainActivity:
public class SMS extends Activity
{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button ) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);
btnSendSMS.setOnClickListener(new View .OnClickListener()
{
public void onClick(View v)
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
sendSMS(phoneNo, message);
else
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
});
}
}
sendSMS() function:
public class SMS extends Activity
{
//...
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
//...
}
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, SMS.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
}
//---sends an SMS message to another device---:
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
Code source: http://mobiforge.com/developing/story/sms-messaging-android
这篇关于sendTextMessage(phoneNumber的,空,消息,NULL,NULL);总是返回成功,即使消息未发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文