嗨,你错过了在你的意图中提到brodcast接收者名字.

请尝试以下代码,它对我来说工作正常..

ArrayList sentPendingIntents = new ArrayList();

ArrayList deliveredPendingIntents = new ArrayList();

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 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(mContext, "SMS sending failed...",

Toast.LENGTH_SHORT).show();

}

为下面发送的短信创建一个Boardcast接收器.

public class SmsSentReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

switch (getResultCode()) {

case Activity.RESULT_OK:

Toast.makeText(context,

"SMS Sent" + intent.getIntExtra("object", 0),

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;

}

}

为sms创建另一个广播接收器,如下所示.

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;

}

}

}

最后在清单中声明接收器.

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐