我怎样才能在采用Android的后台发送短信? [英] How can I send sms messages in the BACKGROUND using Android?

查看:188
本文介绍了我怎样才能在采用Android的后台发送短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从iPhone开发,你不能没有要求用户确认发送发送后台短信来了。可以通过手机短信发送在后台的android,这样无需用户干预需要?

I am coming from iphone development where you cannot send an SMS in the background without asking the user to confirm the send. Can sms be sent in the background in android so that no user intervention is need?

推荐答案

发送短信与短信送达通知作为敬酒。

Send SMS with SMS-Delivery notification as toast.

方法调用如下。

sendSMS("98********","This is test message");

方法如下签名。

method signature as below.

/*
 * 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如下图所示。

Now two more classes SmsDeliveredReceiver,SmsSentReceiver as below.

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。

Now 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;
    }
}

}

现在权限打开你的Andr​​oidManifest.xml中添加以下行

Now Permissions open your AndroidManifest.xml and add below line

<uses-permission android:name="android.permission.SEND_SMS"/>

和对其做.......

and its done.......

这篇关于我怎样才能在采用Android的后台发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆