我使用以下代码通过Intent发送短信(无法请求许可,因此smsmanager不是一个选项)
//Code from this question
// <http://stackoverflow.com/questions/20079047/android-kitkat-4-4-hangouts-cannot-handle-sending-sms-intent>
private void sendsms(String toContact, String text){
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact)));
intent.putExtra("sms_body", text);
if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
{
intent.setPackage(defaultSmsPackageName);
}
}
else
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", toContact);
intent.putExtra("sms_body", text);
}
this.startActivity(intent);
}
我正在for
循环中调用它:
for(int i = 0; i<4 ;i++) {
sendsms(phoneNo[i],smsBody[i]);
}
现在问题是当用户到达这一行时,void将被调用4次,但是用户只能看到设备默认消息应用程序中的最后一条消息准备好发送,但要到达其他消息,用户应该按回设备,如果没有,他/她将永远不会看到其他消息。
我需要做的是使用像startActivityForResult();
这样的方法,因此每次用户发送消息时,他都会被重定向到我的应用程序,然后我的应用程序会为下一条短信启动另一个活动。
任何想法?
提前致谢
答案 0 :(得分:0)
在for循环中调用此方法
完整方法
/*
* BroadcastReceiver mBrSend; BroadcastReceiver mBrReceive;
*/
private void sendSMS(String phoneNumber, String message) {
ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0,
new Intent(mContext, SmsSentReceiver.class), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,
new Intent(mContext, SmsDeliveredReceiver.class), 0);
try {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> mSMSMessage = sms.divideMessage(message);
for (int i = 0; i < mSMSMessage.size(); i++) {
sentPendingIntents.add(i, sentPI);
deliveredPendingIntents.add(i, deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,
sentPendingIntents, deliveredPendingIntents);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "SMS sending failed...",Toast.LENGTH_SHORT).show();
}
}
现在还有两个类SmsDeliveredReceiver,SmsSentReceiver如下所示。
public class SmsDeliveredReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
}
现在发送SMSSentReceiver。
public class SmsSentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "SMS generic failure", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "SMS no service", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "SMS null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "SMS radio off", Toast.LENGTH_SHORT).show();
break;
}
}
}
现在权限打开AndroidManifest.xml并添加到行
<uses-permission android:name="android.permission.SEND_SMS"/>
及其完成.......
答案 1 :(得分:0)
首先,您需要在手机上创建一个字符串,因为必须使用“;”作为分隔符,但要小心三星,在这些设备中,您必须使用“,”。
因此您的代码必须与此代码相似:
private void sendSms(String phone1, String phone2){
String separator = "; "
if (Build.MANUFACTURER.toLowerCase().contains("samsung"))
separator = ", "
String phones = phone1 + separator + phone2...
Intent intentSms = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phones, null));
intentSms.putExtra("sms_body","your text") // Just if you want to add text
startActivity(intentSms);
}