1

我在发送短信接收器时遇到问题。我在Manifest权限和意图过滤器中设置,在Activity- 创建短信并BroadcastReceiver捕获意图,如教程

这是来自的代码Activity

        try {
            String DELIVERED = "SMS_RECEIVED";
            PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(contact.getPhone(), null, message, null, deliveredPI);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Sms do not sent");
        }

这是权限Manifest

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
    ...
    <receiver android:name="com.uniquare.sms.globals.DeliveredReceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

和一部分Receivers

 public class DeliveredReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "DeliveredReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(LOG_TAG, "onReceive()");
    //get the SMS message passed in
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String phone = "";
    if (bundle != null) {
        //retrieve the SMS message received
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
                    .....

对我来说有些奇怪。如果我向我当前的设备发送短信,BroadcastReceiver工作正常。但是,如果我从联系人列表发送给其他用户BroadcastReceiver根本不起作用。

我尝试设置android.provider.Telephony.SMS_RECEIVED而不是SMS_RECEIVEDin Activity PendingIntent

            String DELIVERED = "android.provider.Telephony.SMS_RECEIVED";
            PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);

在这种情况下DeliveredReceiver是有效的,onReceive被调用,但是当我尝试从中获取时pdusObject[] pdus = (Object[]) bundle.get("pdus");我得到了null.

你有同样的问题吗?你能建议我吗?

4

0 回答 0