【问题标题】:Information from BroadcastReceiver to specific Activity via external class通过外部类从 BroadcastReceiver 到特定 Activity 的信息
【发布时间】:2013-08-03 18:38:08
【问题描述】:

我有“ComposeActivity”,它在 onClick 之后调用“SendSMS”方法,而不是调用 SMS 类中的方法。我还注册了两个 BroadcastReceiver:SmsDeliveredReceiver 和 SmsSentReceiver,类似于:https://stackoverflow.com/a/17164931/1888738。我如何通知 ComposeActivity,短信已成功发送,并且该活动可以清理一些 EditText,并可能显示带有短信发送与否信息的面包块(以及为什么)?我的代码:http://pastebin.com/LNRuSeBu

【问题讨论】:

    标签: android android-activity broadcastreceiver


    【解决方案1】:

    如果您在发送或不发送 SMS 消息时需要处理接收者。您可以通过创建一个意图并调用intent.setComponent 来指定意图应该去哪里来修改两个接收者的 onReceive 以发送和意图到 ComposeActivity。一些数据告诉 ComposeActivity 尝试发送消息的结果。

    更新:

     public void onReceive(Context context, Intent arg1) {
        Intent i = new Intent(action);
        i.setComponent(new ComponentName("com.mypackage.compose","ComposeActivity"));
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.d(getClass().getSimpleName(), "SMS delivered");
                intent.setAction("com.mypackage.compose.SMS_SENT"); // String you define to match the intent-filter of ComposeActivity. 
                break;
            case Activity.RESULT_CANCELED:
                Log.d(getClass().getSimpleName(), "SMS not delivered");
                intent.setAction("com.mypackage.compose.SMS_FAILED"); // String you define to match the intent-filter of ComposeActivity. 
    
                break;
        }
         startActivity(intent); // you may not necessarily have to call startActivity but call whatever method you need to to deliver the intent.
    
    }
    

    此时,只需通过清单或以编程方式将意图过滤器和接收器添加到您的撰写活动中即可。您的来电。我使用的字符串是组成的,但您可以选择一个现有的意图操作字符串或声明您在意图过滤器中使用的字符串。再由你决定。查看有关向Android explicit intent with target component 等组件发送显式意图的问题也可能会有所帮助 或查看android docs

    【讨论】:

    • 你能帮我举个例子吗?
    • 我正在尝试以不同的方式从几个小时开始,但它仍然不起作用。在您的指导下,我明白了,我需要的是“隐式意图”,因为有时信息会发送到 ComposeActivity,有时会发送到 SmsListActivity。回到问题:我不能使用你的方法“BroadcastReceiver”或代码:'code' Intent intent = new Intent();意图.setAction("动作");发送广播(意图); “无法解析方法 sendBroadcast ...” 与您的方法相同。解析“ComponentName”时出现问题。
    【解决方案2】:

    好的,经过5个小时的尝试,我已经解决了这个问题:

    在 onReceive 中的 BroadcastReceiver 中:

    Intent intent = new Intent();
    intent.setAction("SOMEACTION");
    context.sendBroadcast(intent);
    

    在活动中:

    public BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("SOMEACTION")) {
                Log.d(TAG, "Sent");
            }
        }
    };
    

    在 onCreate Activity 中我注册了 BroadcastReceiver:

    registerReceiver(receiver, new IntentFilter("SOMEACTION"));
    

    就是这样……

    【讨论】:

    • 所以这基本上就是我所描述的你所做的......无论如何很高兴你解决了你的问题/
    猜你喜欢
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多