我正在从系统接收到sms_广播,并试图在其他广播接收器接收到它之前更改意图中的值(例如默认消息应用程序)
但是,无论我尝试以何种方式更改意图,广播侦听器都看不到更新的值。因为系统不允许你发送短信接收的广播,所以我不能中止这一次并重新发送一次。我是做错了什么,还是没办法?
//我尝试过的一些示例代码

Intent updatedIntent = changeIncomingIntent(intent, context);
intent.putExtras(updatedIntent.getExtras());
intent.replaceExtras(updatedIntent.getExtras());

最佳答案

你不能改变收到的意图,但是如果你…呵呵……打算修改收到的短信,因为它是一个有序的广播,您可以停止广播,修改意图的附加内容,然后重新广播它:
在application标记前向applicationmanifest.xml添加以下内容:

<uses-permission android:name="android.permission.BROADCAST_SMS" />

请注意,这将显示在应用程序的“权限”页的“播放存储区”中。准备好解释。
并将此添加到接收器中的intent filter标记:
android:priority="2147483647"

这是Java中可能的最高整数,确保接收者获得第一优先级。请注意,其他应用程序(尤其是go-sms)中的接收器可能使用相同的策略,这可能意味着两个接收器最终会为一条消息而发生冲突。如果发生这种情况,您可能需要卸载其他应用程序。
然后在你的接收器里:
public void onReceive(Context context, Intent intent){

    //Messages are delivered as extras in the intent

    Bundle bundle = intent.getExtras();

    //If this intent was rebroadcasted already, ignore it.
    if(bundle.getBooleanExtra("rebroadcast", false)
         return;

    //Abort the broadcast
    abortBroadcast();

    //Some examples use regular arrays,
    // but I prefer a more sophisticated solution.
    ArrayList<SmsMessage> msgs = new ArrayList<SmsMessage>();

    //Check to make sure we actually have a message
    if(bundle != null){

        //Get the SMS messages
        //They are delivered in a raw format,
        //called Protocol Description Units or PDUs

        //Messages can sometimes be delivered in bulk,
        //so that's why it's an array

        Object[] pdus = (Object[]) bundle.get("pdus");

        //I prefer the enhanced for-loop. Why reinvent the wheel?
        for(Object pdu : pdus)
            msgs.add(SmsMessage.createFromPdu((byte[])pdu));

        //Do stuff with the message.
        ArrayList<SmsMessage> newMessages = smsReceived(msgs);

        //Convert back to PDUs
        ArrayList<Object> pdus;
        Iterator<SmsMessage> iterator = newMessages.iterator();

        //Iterate over the messages and convert them
        while(iterator.hasNext()){
            pdus.add(iterator.next().getPdu())
        }

        //Convert back to an Object[] for bundling
        Object[] pduArr = pdus.toArray();

        //Redefine the intent.

        //It already has the broadcast action on it,
        //so we just need to update the extras
        bundle.putExtra("pdus", pduArr);

        //Set a rebroadcast indicator so we don't process it again
        //and create an infinite loop
        bundle.putExtra("rebroadcast", true);

        intent.putExtras(bundle);

        //Finally, broadcast the modified intent with the original permission
        sendOrderedBroadcast(intent, "android.permission.RECEIVE_SMS");

    else{
        //For some reason, there was no message in the message.
        //What do you plan to do about that? I'd just ignore it.
    }
}

虽然这是一个功能示例,但我建议在新线程中执行大部分操作,以便在消息处理是处理器密集型的情况下不会阻塞ui线程。例如,我之所以在我的系统中这样做,是因为我使用了很多正则表达式来操作消息,以及执行一些与消息相关的非常繁重的数据库查询。
如果我做错了什么或者本可以做得更好,请不要犹豫纠正我。大部分代码都在我自己的项目中,我希望能够保证它的功能。
祝你今天愉快。

关于android - 在有序的BroadcastReceivers android之间更新Intent Extras,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6391608/

10-10 23:06