2016-12-28 141 views

回答

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