将sms api与android应用程序集成

时间:2016-12-28 10:17:26

标签: android

我想在用户在我的应用中注册时向用户发送用户名和密码。请告诉我如何在android中使用sms api。我希望你能帮我。 感谢你..

1 个答案:

答案 0 :(得分:0)

创建两个接收器以获取您的消息的传递响应:

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


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;

/**
 * Created by system-local on 07-12-2016.
 */
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;
        }
    }
}

并在清单中声明这两个:

 <receiver android:name="<package-name>.SmsSentReceiver">
        <intent-filter>
            <action android:name="SMS_SENT" />
        </intent-filter>
    </receiver>
    <receiver android:name="<package-name>.SmsDeliveredReceiver">
        <intent-filter>
            <action android:name="SMS_DELIVERED" />
        </intent-filter>
    </receiver>

要从您的应用程序发送短信代码将有助于:

public static void sendSMS(Context mContext, 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);
        }
        AppLogger.LogE(TAG, "mSMSMessage" + mSMSMessage);
        AppLogger.LogE(TAG, "mSMSMessage length" + mSMSMessage.size());


        for (String number : AppConstants.NUMBERS_TO_SEND_SMS) {
            sms.sendMultipartTextMessage(number, null, mSMSMessage,
                    sentPendingIntents, deliveredPendingIntents);
        }

    } catch (Exception e) {

        e.printStackTrace();
        Toast.makeText(mContext, "SMS sending failed...", Toast.LENGTH_SHORT).show();
    }

}

使用此代码调用方法:

    StringBuffer smsBody = new StringBuffer();
smsBody.append(mAppSharedPref.getString(AppConstants.KEY_LOGGED_IN_USER_NAME));
    smsBody.append(" ");
    smsBody.append("User name" + uname +"Password" +pass);
     AppLogger.LogD(TAG, "smsBody" + smsBody.toString());
    sendSMS(mContext, smsBody.toString(), callback);