我这里有一些代码发送消息:
SmsManager sms = new SmsManager.getDefault();
sms.sendTextMessage("911", null, "HALP!", PendingIntent, null);
Run Code Online (Sandbox Code Playgroud)
Developer.android说PendingIntent:
如果没有
NULL这个PendingIntent当成功发送的消息是广播,还是失败.结果代码将Activity.RESULT_OK成功,或者其中一个错误:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU对于
RESULT_ERROR_GENERIC_FAILURE该sentIntent可包括额外的"的errorCode"含有的无线技术特定的值,通常只对故障排除.基于每个应用程序的SMS控制检查
sentIntent.如果sentIntent是NULL,则将针对所有未知应用程序检查呼叫者,这导致在检查时段中发送较少数量的SMS.
我的问题是:如何让一个PendingIntent喂到sendTextMessage,仅仅显示了一个Toast说法或者消息被发送或没有?
谢谢.
这可以简单地通过注册您可以实现PendingIntent与BroadcastReceiver该通知您,当您的邮件发送成功,当它交付给收件人.请按照以下步骤_
registerReceiver,因此,他们的 广播接收器得到通知并做needfull.我已经构建了一个简单的类来完成我上面解释过的工作.你可以通过这门课程来完全理解它_
public class SendDeliverSMSActivity extends Activity {
Button btnSendSMS;
/** 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);
btnSendSMS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Your method that send sms
sendSMS("5556", "Hi You got a message!");
}
});
}
/**
* ---sends an SMS message to another device---
*
* @param phoneNumber
* @param message
*/
private void sendSMS(String phoneNumber, String message) {
// Intent Filter Tags for SMS SEND and DELIVER
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
// STEP-1___
// SEND PendingIntent
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
// DELIVER PendingIntent
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
// STEP-2___
// SEND BroadcastReceiver
BroadcastReceiver sendSMS = 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;
}
}
};
// DELIVERY BroadcastReceiver
BroadcastReceiver deliverSMS = 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;
}
}
};
// STEP-3___
// ---Notify when the SMS has been sent---
registerReceiver(sendSMS, new IntentFilter(SENT));
// ---Notify when the SMS has been delivered---
registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这能帮到您!:)
| 归档时间: |
|
| 查看次数: |
2695 次 |
| 最近记录: |