我来自 iphone 开发,如果不要求用户确认发送,您将无法在后台发送 SMS。可以在 android 的后台发送短信,这样就不需要用户干预了吗?
问问题
23811 次
4 回答
28
发送带有 SMS-Delivery 通知的 SMS 作为 toast。
方法调用如下。
sendSMS("98********","This is test message");
方法签名如下。
/*
* 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"/>
它完成了.......
于 2012-11-21T10:17:54.413 回答
19
是的,您可以使用:
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(number, null, message, null, null);
于 2011-06-15T17:10:34.473 回答
2
于 2011-06-15T17:03:20.387 回答
1
最佳答案很好,但在 API 级别 23 以上,您需要务实地获得许可。否则每次都会提示权限。
private static final int PERMISSION_REQUEST_CODE = 1;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_DENIED) {
Log.d("permission", "permission denied to SEND_SMS - requesting it");
String[] permissions = {Manifest.permission.SEND_SMS};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
}
于 2018-01-23T12:45:13.583 回答