发送未收到的SMS广播



我必须发送短信并在交付时采取一些行动,我使用广播待定的意图来广播状态如下。

BroadcastReceiver smsStatusReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();}
};
@Override
    protected void onCreate(Bundle savedInstanceState) {
        mBroadcastManager = LocalBroadcastManager.getInstance(this);
        mBroadcastManager.registerReceiver(smsStatusReceiver, new IntentFilter(ACTION_SENT));
        mBroadcastManager.registerReceiver(smsStatusReceiver, new IntentFilter(ACTION_DELIVERED));
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Utils.sendSMS(MainActivity.this, "123456", "test", ACTION_SENT, ACTION_DELIVERED);
            }
        });
    }

在Utils

public static void sendSMS(Activity activity, String destination, String text, String sentAction, String deliveryAction) {    
        PendingIntent sentPI = null;
        PendingIntent deliveredPI = null;
        if (!TextUtils.isEmpty(sentAction)) {
            sentPI = PendingIntent.getBroadcast(activity, 0, new Intent(sentAction), 0);
        }
        if (!TextUtils.isEmpty(deliveryAction)) {
            deliveredPI = PendingIntent.getBroadcast(activity, 500, new Intent(deliveryAction), 0);
        }
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(destination, null, text, sentPI, deliveredPI);
    }

尽管成功发送了SMS,但从文档中找不到任何并不简单的东西,但从未调用OnReceive((。知道发生了什么事吗?

您是否在清单类中添加了广播员?

<receiver android:name=".receiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
</receiver>

使用sms.sendTextMessage(phoneNumber, null, message, sentPI, null);发送SMS时,您可以在接收时提供待处理意图(sentPI(。

If not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu").

您可以提供广播员作为垂悬的人:

Intent intentWithData = new Intent(context, SMSDeliveredBroadcastReceiver.class);
intentWithData.putExtra("number",number);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 7, intentWithData, 0);

最新更新