Android:创建持久事件监听器

发布于 2024-12-07 19:34:04 字数 1354 浏览 0 评论 0原文

我试图弄清楚如何实现事件监听器(不确定这是否是正确的术语。)我希望该服务在我的应用程序启动后监听手机的电源状态。我不确定 android 如何处理这种情况,所以不知道要搜索什么。我一直在使用这段使用广播接收器的代码:

 BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            unregisterReceiver(this);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            if (plugged == BatteryManager.BATTERY_PLUGGED_AC) {
                // on AC power
                 Toast.makeText(getApplicationContext(), "AC POWER", Toast.LENGTH_LONG).show();
            } else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
                // on USB power
             Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
                 startActivity(alarmClockIntent);
            } else if (plugged == 0) {
                Toast.makeText(getApplicationContext(), "On Battery", Toast.LENGTH_LONG).show();
            } else {
                // intent didnt include extra info
            }
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(receiver, filter);

该代码工作正常。当我打开应用程序时,它会显示手机电源的当前状态。

这就是我想要做的:

  • 当用户启动应用程序时,它实际上打开了服务
  • 用户可以继续使用手机,但是一旦插入,我的服务就会捕获该情况并使用上面的代码

< em>如何调整此代码以实现上述目标?

I am trying to figure out how to implement an event listener (unsure if this is the proper term.) I want the service, once my app is launched, to listen for the phones power status. I am uncertain to as how android handles this situation so don't really know what to search for. I've been working with this code that uses a broadcast receiver:

 BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            unregisterReceiver(this);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            if (plugged == BatteryManager.BATTERY_PLUGGED_AC) {
                // on AC power
                 Toast.makeText(getApplicationContext(), "AC POWER", Toast.LENGTH_LONG).show();
            } else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
                // on USB power
             Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
                 startActivity(alarmClockIntent);
            } else if (plugged == 0) {
                Toast.makeText(getApplicationContext(), "On Battery", Toast.LENGTH_LONG).show();
            } else {
                // intent didnt include extra info
            }
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(receiver, filter);

The code works fine. When I open my app it will toast what the current status of the phone power is.

Here is what I am trying to do:

  • When the user launches the app, it is effectively turning on the service
  • The user can go about using the phone, but once it is plugged in, my service will catch that and use the code above

How do I adapt this code to achieve the objectives above?

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

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

发布评论

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

评论(1

嗳卜坏 2024-12-14 19:34:04

您可以通过删除该线来保持电池状态侦听器打开

unregisterReceiver(this);

。这样,即使应用程序未在前台运行,应用程序也将继续在后台侦听电源状态更改。请注意,在某些时候,您可能仍想注销接收器。您可能希望允许用户通过设置来控制它。

另请注意,您的代码在以下代码中包含接收器中的启动活动:


else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
    // on USB power
    Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
    startActivity(alarmClockIntent);
} 

如果您的活动位于后台,则它无法启动另一个活动。请参阅 ​​这个问题 - 如何开始当主活动在后台运行时的活动?,接受的答案有关于如何处理需要从后台启动活动的情况的建议

You could keep the listener on for the battery status by removing the line

unregisterReceiver(this);

This way, the app will continue to listen to power status change in the background even though that the app is not running in the foreground. Note that at some point, you might still want to unregister your receiver. You probably want to allow the user to control that via settings.

One other note, your code contains starting activity in the receiver in below code:


else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
    // on USB power
    Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
    startActivity(alarmClockIntent);
} 

If your activity is in the background then it can't start another activity. See this SO Question - how to start activity when the main activity is running in background?, the accepted answer has suggestion on how to handle situation that requires starting activity from the background

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