2

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

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

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

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

2 回答 2

4

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

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

<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 线程。例如,我这样做是因为我使用了大量的正则表达式来操作消息,以及执行一些与消息相关的非常繁重的数据库查询。

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

祝你今天过得愉快。

于 2012-10-24T13:21:44.067 回答
0

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

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

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

于 2011-06-17T21:14:25.397 回答