广播接收器只能在没有开始新活动的情况下才能将信息发送到活动?

发布于 2025-02-06 07:35:06 字数 213 浏览 5 评论 0原文

我有一个广播接收器,该接收器会在地理上事件上触发,并且使用服务器“时钟”或“时钟”。如果我的应用程序的“出勤”活动已经打开仅开放。

我想象的方式是在广播接收器上向活动发送意图,但是命名“ startactivity()”听起来并不令人鼓舞,除非我可以通过任何特殊的旗帜来防止启动尚未开放的活动 - 我似乎找不到任何。

另一个选择是在活动开放时不断对价值进行投票,但似乎并不最佳有意图。

I have a broadcast receiver that gets triggered on geofencing events and either "clocks in" or "clocks out" with the server. If my application's "Attendance" activity is already open I would like it to display the clocking status change but I don't want the Broadcast Receiver to start the activity if it's not open - in other words display the change "live" while the activity is open only.

The way I imagine doing this is with the Broadcast Receiver sending an Intent to the activity but name "startActivity()" doesn't sound encouraging unless there are any special flags I can pass to prevent starting an Activity that isn't already open - I can't seem to find any.

The other option would be to constantly poll the value while the activity is open but it doesn't seem optimal so I would only use it if there wasn't another way and I can't think of a reason why it couldn't be possible with Intents.

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

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

发布评论

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

评论(2

不如归去 2025-02-13 07:35:07

有几种不同的方法可以完成相同的任务。一个人像以下示例一样注册听众:

MainActivity

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Receiver.setOnReceiveListener(new Receiver.OnReceiveListener() {
            public void onReceive(Context Context, Intent intent)
            {
                //Do something.
            }
        });
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Receiver.setOnReceiveListener(null);
    }
}

接收器

public class Receiver extends BroadcastReceiver
{
    private static OnReceiveListener static_listener;

    public static abstract interface OnReceiveListener
    {
        public void onReceive(Context context, Intent intent);
    }

    public static void setOnReceiveListener(OnReceiveListener listener)
    {
        static_listener = listener;
    }


    @Override
    public final void onReceive(Context context, Intent intent)
    {
        if(static_listener != null) {
            static_listener.onReceive(context, intent);
        }
    }
}

There are several different ways to accomplish the same task. One is registering a listener like the following example:

MainActivity

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Receiver.setOnReceiveListener(new Receiver.OnReceiveListener() {
            public void onReceive(Context Context, Intent intent)
            {
                //Do something.
            }
        });
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Receiver.setOnReceiveListener(null);
    }
}

Receiver

public class Receiver extends BroadcastReceiver
{
    private static OnReceiveListener static_listener;

    public static abstract interface OnReceiveListener
    {
        public void onReceive(Context context, Intent intent);
    }

    public static void setOnReceiveListener(OnReceiveListener listener)
    {
        static_listener = listener;
    }


    @Override
    public final void onReceive(Context context, Intent intent)
    {
        if(static_listener != null) {
            static_listener.onReceive(context, intent);
        }
    }
}
許願樹丅啲祈禱 2025-02-13 07:35:07

只需让您的broadcastreceiver发送广播意图即可。您的活动应从此广播意图中注册一个侦听器,如果它触发了,则可以更新UI。


这是一个示例:

在您的活动中声明私有成员变量:

private BroadcastReceiver receiver;

activity.oncreate()中,注册broadcastreceiver

IntentFilter filter = new IntentFilter();
filter.addAction("my.package.name.CLOCK_STATUS_CHANGE");

receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Here you can update the UI ...
    }
};
registerReceiver(receiver, filter);

in in OnDestroy()您可以取消注册(可能不是必需的,但更清洁):

if (receiver != null) {
   unregisterReceiver(receiver);
   receiver = null;
}

在您的broadcastreceiver中检测到地理范围事件的,您应该创建并发送广播::

Intent broadcastIntent = new Intent("my.package.name.CLOCK_STATUS_CHANGE");
sendBroadcast(broadcastIntent);

Just have your BroadcastReceiver send a broadcast Intent. Your Activity should register a listener from this broadcast Intent and if it gets triggered, it can update the UI.


Here's an example:

Declare a private member variable in your Activity:

private BroadcastReceiver receiver;

In Activity.onCreate(), register the BroadcastReceiver:

IntentFilter filter = new IntentFilter();
filter.addAction("my.package.name.CLOCK_STATUS_CHANGE");

receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Here you can update the UI ...
    }
};
registerReceiver(receiver, filter);

And in onDestroy() you can unregister it (probably not necessary, but cleaner):

if (receiver != null) {
   unregisterReceiver(receiver);
   receiver = null;
}

In your BroadcastReceiver that detects the geofencing event, you should create and send a broadcast Intent:

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