在Android中动态创建BroadcastReceiver

发布于 2024-11-30 09:31:27 字数 604 浏览 1 评论 0原文

我正在构建一个 Android 应用程序,它通过短信向用户提供信息。用户将短信发送到应用程序中硬编码的预定号码,然后收到响应。

为了实现这一点,我在 Android 清单中使用广播接收器。

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

如果我创建一个新类 SMSReciever,那么当收到响应时,我会以某种方式将信息发送回用户在屏幕上打开的活动。我是否可以在我的主要活动中动态地执行此操作,以便使应用程序更具交互性?

我已经了解了 Context.registerReciever 方法,但是如何提及接收者的优先级?如果接收者没有更高的优先级,那么消息传递应用程序可能会阻止广播到达我的应用程序。有没有人遇到过类似问题的解决方案?

I am building an Android app that works by providing information via SMS to users.Users send an SMS to a predetermined number hardcoded in the APP and in turn receive a response.

To achieve this, I am using a broadcast Receiver in my Android Manifest

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

If I create a new class SMSReciever, then when a response is received, I would to somehow send the information back to the activity which the user has open on the screen. Is it possible I can do this in my main activity dynamically so that the app can be made more interactive?

I have learnt about the Context.registerReciever method, but how do I mention the priority for the receiver? If the receiver does not have a higher priority, then the messaging app might stop the broadcast from reaching my app. Has anyone come across a solution for something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归属感 2024-12-07 09:31:27

我使用以下代码来实现我想要的!

IntentFilter fp = new IntentFilter();
fp.addAction("android.provider.Telephony.SMS_RECEIVED");
fp.setPriority(18);

//--- when the sms is received ---
registerReceiver(new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent arg1) { 
        //Processing for received broadcast happens here              
    }
}, fp);

I used the following code to achieve what I was looking for!

IntentFilter fp = new IntentFilter();
fp.addAction("android.provider.Telephony.SMS_RECEIVED");
fp.setPriority(18);

//--- when the sms is received ---
registerReceiver(new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent arg1) { 
        //Processing for received broadcast happens here              
    }
}, fp);
烟织青萝梦 2024-12-07 09:31:27

我自己没有测试过,但我通过 Android 源代码进行了快速搜索。

优先级被解析并保存到 IntentFilter 中(请参阅 代码)。因此,您可以创建 IntentFilter 并使用其 setPriority() 函数设置广播接收器的优先级。

 IntentFilter filter = new IntentFilter(...);
 filter.setPriority(receiverPriority);// <-----
 context.registerReceiver(filter, mBroadcastReceiver);

I haven't tested this myself, but I've made a quick search via Android source code.

The priority is parsed and saved into IntentFilter (see code). So you can create IntentFilter and use its setPriority() function to set priority of your broadcast receiver.

 IntentFilter filter = new IntentFilter(...);
 filter.setPriority(receiverPriority);// <-----
 context.registerReceiver(filter, mBroadcastReceiver);
弄潮 2024-12-07 09:31:27

我将通过使用诸如 com.example.intent.VERIFICATION_RECEIVED 之类的自定义意图操作来解决此问题,并从 SMSReciever 广播此操作,然后让您的 Activity使用 BroadcastReceiver 监听它。

IntentFilter filter = new IntentFilter(PageFragment.ACTION_LOADING_DONE);
registerReceiver(new MyReceiver(), filter);

使用这种方法,您可以将 SMS 处理保留在 SMSReciever 中,并且您的 Activity 不需要关心如何处理 SMS。

I would solve this by using a custom intent action like com.example.intent.VERIFICATION_RECEIVED and the broadcasting this from SMSReciever and then having your Activity listen for it with a BroadcastReceiver.

IntentFilter filter = new IntentFilter(PageFragment.ACTION_LOADING_DONE);
registerReceiver(new MyReceiver(), filter);

Using this approach you can keep your SMS handling in SMSReciever and your Activity doesn't need to care about how to handle a SMS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文