当用户单击我的按钮时,它执行两个功能:发送 SMS 和发送电子邮件。
当我单击此按钮时,正在发送 SMS,突然弹出电子邮件选择客户端窗口。我希望仅在完成 SMS 发送功能后才显示电子邮件客户端选择器窗口。
我应该如何更改我的代码?
Button hi= (Button) findViewById(R.id.button1);
hi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendsms();
sendemail();
}
private void sendemail() {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to1});
email.putExtra(Intent.EXTRA_CC, new String[]{ to2});
email.putExtra(Intent.EXTRA_BCC, new String[]{to3});
email.putExtra(Intent.EXTRA_BCC, new String[]{to4});
email.putExtra(Intent.EXTRA_BCC, new String[]{to5});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, emailmessage);
//need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
sendsms()
{
String receipentsNumber[] = {"111","222","333","444","555"};
for (int i = 0; i < receipentsNumber.length; i++) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(receipentsNumber[i], null, message, null,
null);
System.ot.println(getApplicationContext(), "SMS Sent to" + " " + receipentsNumber[i], Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!", Toast.LENGTH_LONG)
.show();
e.printStackTrace();
}
}
}
});
最佳答案
根据您的代码,您必须等待 SMS 状态,或者我们可以说 Success 或 Failed Call back。您可以使用 Broadcast Receiver它将处理事件并返回一些反馈。
并且您可以在短信成功后调用您的电子邮件功能。
Check Reference Example这对你来说是完美的。
如果需要监控短信发送过程的状态,其实可以使用两个Pending Intent对象连同两个 BroadcastReceiver
对象,像这样:
一个用于发送短信,第二个用于该交付状态。
//---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);
}
关于android - 等待进程完成以启动另一个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21058532/