如何为广播接收器定义 IntentFilter

发布于 2024-12-22 11:44:50 字数 463 浏览 0 评论 0原文

因此,我试图制作一个应用程序,在收到每条短信时都会显示一条祝酒消息。我希望应用程序仅显示 toast,并且我当前使用此代码来显示它,但在应用程序运行时出现异常错误。我正在使用这段代码,

BroadcastReceiver BR = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText( context, "testing", Toast.LENGTH_SHORT ).show();

        }
    };


    IntentFilter filter = new  IntentFilter();

    registerReceiver(BR, filter);

我很确定它是意图过滤器,但我不确定。

So I am attempting to make an application that will show a toast message when every a text message is received. I want the application to only show the toast, and I currently using this code to show it, but I get an exception error when the application runs. I am using this code

BroadcastReceiver BR = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText( context, "testing", Toast.LENGTH_SHORT ).show();

        }
    };


    IntentFilter filter = new  IntentFilter();

    registerReceiver(BR, filter);

I'm pretty sure it's the intent filter, but I'm not sure.

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

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

发布评论

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

评论(2

和影子一齐双人舞 2024-12-29 11:44:50

您必须在清单中输入广播接收器的条目,然后添加我们为活动添加的意图过滤器。这在运行时也是可能的。

You have to make the entry of your broadcastreceiver in manifest then add the intent filter as we add for our activities.It is possible on runtime too.

紙鸢 2024-12-29 11:44:50

您应该创建扩展 BroadcastReceiver 的单独类
然后重写 onRecieve 方法。

在这里,AlarmMain.this 是主类,
OneShotAlarm.class 扩展了 BroadcaseReceiver :

@Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText( context, "testing", Toast.LENGTH_SHORT ).show();

        }

您可以按照以下方式调用 onRecieve() :

Intent intentAlarm = new Intent(AlarmMain.this, OneShotAlarm.class);
operation = PendingIntent.getBroadcast(AlarmMain.this, 0, intentAlarm, 0);

alarmManager  = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, objDate.getTime(), operation);

您可以尝试以下操作,但它是可选的(将以下标记包含到 AndroidManifest.xml 中)

<receiver 
    android:name="com.android.alarm.OneShotAlarm"
    android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

You should create seperate class which extends BroadcastReceiver
and then override onRecieve method.

Here, AlarmMain.this is main class and
OneShotAlarm.class which extends BroadcaseReceiver :

@Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText( context, "testing", Toast.LENGTH_SHORT ).show();

        }

You can call onRecieve() as per following:

Intent intentAlarm = new Intent(AlarmMain.this, OneShotAlarm.class);
operation = PendingIntent.getBroadcast(AlarmMain.this, 0, intentAlarm, 0);

alarmManager  = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, objDate.getTime(), operation);

You can try following but it is optional (include following tag into AndroidManifest.xml)

<receiver 
    android:name="com.android.alarm.OneShotAlarm"
    android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文