【问题标题】:Update Intent extras between ordered BroadcastReceivers android在已订购的 BroadcastReceivers android 之间更新 Intent extras
【发布时间】:2011-06-17 20:43:14
【问题描述】:

我目前正在从系统中获取 SMS_Received 广播,并试图在其他广播接收者得到它之前更改意图中的值,(例如默认消息传递应用程序)

但是,无论我尝试以何种方式更改意图,广播侦听器都看不到更新后的值。而且由于系统不允许您发送 SMS_Received 广播,因此我无法中止此广播并重新发送广播。是我做错了什么还是没有办法做到这一点?

//我尝试过的一些示例代码

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

【问题讨论】:

    标签: android android-intent broadcastreceiver edit extras


    【解决方案1】:

    你不能改变接收到的intent,但是如果你……呵呵……打算修改接收到的短信,因为它是一个有序广播,你可以停止广播,修改意图的附加内容,然后重新广播:

    将以下内容添加到 ApplicationManifest.xml 的 Application 标记之前:

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

    请注意,这将显示在 Play 商店中您应用的权限页面上。准备好解释它。

    并将其添加到接收器中的 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 线程。例如,我在自己的系统中这样做是因为我使用了很多正则表达式来处理消息,以及执行一些与消息相关的非常繁重的数据库查询。

    如果我做错了什么或本可以做得更好,请随时纠正我。大部分代码都在我自己的项目中,我希望能够保证它的功能。

    祝你有美好的一天。

    【讨论】:

      【解决方案2】:

      (1) 你不能只修改 Intent 对象。那是仅适用于您的实例的本地对象。

      (2) 有用于从接收器返回的结果数据的 API(设置结果等),但您只能在广播作为有序广播发送时使用这些 API。很少有人以这种方式发送;文档应该说明它们是什么时候。

      请注意,广播通常是并行的,因此尝试更改其他人将收到的内容是没有意义的——他们很可能与您在同一时间执行。

      【讨论】:

      • SMS_Received 广播按顺序发送出去,那么有没有办法让它改变未来接收者的意图?
      • 不,这对于短信广播是不可能的。
      猜你喜欢
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      相关资源
      最近更新 更多