Android 向收件人发送短信
我正在编写一个应用程序,其中我正在尝试向收件人发送短信,但每当我点击发送时,收到的信息:-短信失败,请稍后再试强> 请看下面的屏幕截图,就像你们所看到的,在这里我正试图向Rahul发送消息 Manifest.xml:Android 向收件人发送短信,android,Android,我正在编写一个应用程序,其中我正在尝试向收件人发送短信,但每当我点击发送时,收到的信息:-短信失败,请稍后再试 请看下面的屏幕截图,就像你们所看到的,在这里我正试图向Rahul发送消息 Manifest.xml: <uses-permission android:name="android.permission.SEND_SMS" /> 请检查以下代码: private TextView name; private ListView list; private
<uses-permission android:name="android.permission.SEND_SMS" />
请检查以下代码:
private TextView name;
private ListView list;
private Database db;
private Contact contact;
Button buttonSend;
EditText textSMS;
private Map<String, AuthenticatorDescription> map = new LinkedHashMap<String, AuthenticatorDescription>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
// bind GUI components
this.name = (TextView) findViewById(R.id.editor_name);
this.list = (ListView) findViewById(R.id.editor_list);
// check if contact id is valid
this.db = new Database(getContentResolver());
int contactId = getIntent().getIntExtra(CONTACT_ID, NO_CONTACT_ID);
this.contact = this.db.getContact(contactId);
if (this.contact == null) {
finish();
}
this.name.setText(this.contact.getName());
// pre-load information about all account types
AuthenticatorDescription[] authTypes = AccountManager.get(this).getAuthenticatorTypes();
for (AuthenticatorDescription authDesc : authTypes) {
this.map.put(authDesc.type, authDesc);
}
// bind list events
this.list.setOnItemClickListener(this);
this.list.setOnCreateContextMenuListener(this);
// create the GUI
updateView();
saveGreeting = (ImageButton) findViewById(R.id.greeting);
saveGreeting.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customGreeting(v);
}
});
buttonSend = (Button) findViewById(R.id.buttonSend);
textSMS = (EditText) findViewById(R.id.editTextSMS);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String recepient = name.getText().toString();
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(recepient, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
私有文本视图名称;
私有列表视图列表;
专用数据库数据库;
私人接触;
按钮发送;
编辑文本短信;
私有映射映射=新建LinkedHashMap();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
//绑定GUI组件
this.name=(TextView)findViewById(R.id.editor\u name);
this.list=(ListView)findViewById(R.id.editor\u list);
//检查联系人id是否有效
this.db=新数据库(getContentResolver());
int contactId=getIntent().getIntExtra(联系人ID,无联系人ID);
this.contact=this.db.getContact(contactId);
if(this.contact==null){
完成();
}
this.name.setText(this.contact.getName());
//预加载有关所有帐户类型的信息
AuthenticatorDescription[]authTypes=AccountManager.get(this).getAuthenticatorTypes();
对于(AuthenticatorDescription authDesc:authTypes){
this.map.put(authDesc.type,authDesc);
}
//绑定列表事件
this.list.setOnItemClickListener(this);
this.list.setOnCreateContextMenuListener(this);
//创建GUI
updateView();
saveGreeting=(ImageButton)findViewById(R.id.greeting);
saveGreeting.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
海关问候(五);
}
});
buttonSend=(按钮)findViewById(R.id.buttonSend);
textSMS=(EditText)findViewById(R.id.editTextSMS);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
String recepient=name.getText().toString();
字符串sms=textSMS.getText().toString();
试一试{
SmsManager SmsManager=smsmsmanager.getDefault();
sendTextMessage(recepient,null,sms,null,null);
Toast.makeText(getApplicationContext(),“SMS已发送!”,
Toast.LENGTH_LONG).show();
}捕获(例外e){
Toast.makeText(getApplicationContext(),
“短信失败,请稍后再试!”,
Toast.LENGTH_LONG).show();
e、 printStackTrace();
}
}
});
}
Android API示例有一个很好的例子,为什么不试试呢
它位于:
...\sdk\samples\android-17\ApiDemos\src\com\example\android\apis\os\SmsMessagingDemo.java
从样本粘贴:
SmsManager sms = SmsManager.getDefault();
List<String> messages = sms.divideMessage(contentTextEdit.getText().toString());
String recipient = recipientTextEdit.getText().toString();
for (String message : messages) {
sms.sendTextMessage(recipient, null, message, PendingIntent.getBroadcast(
SmsMessagingDemo.this, 0, new Intent(ACTION_SMS_SENT), 0), null);
}
阿米特
告诉我你在哪里查这个
在emulator中,sms不发送
这仅在实际设备中发送
请查看下面提到的链接
它工作正常
(一)
(二)
(三)
(四)
(五)
让我告诉你,如果你仍然面临任何问题
为发送和交付注册广播接收器,如下所示
void sendSMS(字符串电话号码、字符串消息)
{/* PendingEvent pi=PendingEvent.getActivity(这个,0, 新意图(此,test.class),0);
SmsManager sms=SmsManager.getDefault(); sms.sendTextMessage(电话号码,空,消息,pi,空);
*/
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---
first = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
boolean status = false;
switch (getResultCode())
{
case Activity.RESULT_OK:
status = true;
/* 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;
}
if(!status)
{
Toast.makeText(getBaseContext(), "SMS Send Failed",
Toast.LENGTH_SHORT).show();
unregisterReceiver(this);
}
}
};
registerReceiver(first,new IntentFilter(SENT));
//---when the SMS has been delivered---
second =new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
smsReply(true);
/*Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();*/
break;
case Activity.RESULT_CANCELED:
smsReply(false);
/* Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();*/
break;
}
unregisterReceiver(this);
}
};
registerReceiver(second, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
注册BroadcastReceiver后,不要忘记在onStop()方法中注销BroadcastReceiver
取消注册接收者(第一);
if(秒!=null)
未注册接收人(第二);
那么堆栈跟踪中有什么内容?@John3136我无法向Rahul发送消息,收到消息:SMS失败,请稍后重试!…这是您编写的错误消息。不是堆栈跟踪。无法与Emulator一起使用。您需要在实际设备中进行测试。我想您已经找到了答案(使用真实设备,而不是Emulator),但您仍然需要学习调试技术,比如在出现异常时查看堆栈跟踪。
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---
first = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
boolean status = false;
switch (getResultCode())
{
case Activity.RESULT_OK:
status = true;
/* 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;
}
if(!status)
{
Toast.makeText(getBaseContext(), "SMS Send Failed",
Toast.LENGTH_SHORT).show();
unregisterReceiver(this);
}
}
};
registerReceiver(first,new IntentFilter(SENT));
//---when the SMS has been delivered---
second =new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
smsReply(true);
/*Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();*/
break;
case Activity.RESULT_CANCELED:
smsReply(false);
/* Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();*/
break;
}
unregisterReceiver(this);
}
};
registerReceiver(second, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
if(first!=null)