我如何使用服务来监控 Android 中的方向变化 [英] How do I use a service to monitor Orientation change in Android

查看:16
本文介绍了我如何使用服务来监控 Android 中的方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个显示倒数计时器的小部件.我让小部件按照我想要的方式工作,直到我将手机从横向翻转为纵向.我的小部件不会更新并在小部件开始时进入其初始状态,直到我的重复警报调用 onupdate.我想在方向更改后手动调用 onupdate 来更新我的小部件我已经研究了一段时间,我发现我需要使用一个服务来监视方向更改并调用我的 onupdate我的小部件.

I'm writing a Widget that will display a countdown timer. I have the widget working the way I want it until I flip the phone from landscape to portrait. My widget does not update and goes to it's initial state at start of the widget until an onupdate is called by my recurring alarm. I would like to call an onupdate manually once the orientation changes to update my widget I've been researching this for a while now and I've found out that I need to use a Service which will monitor the orientation changes and call my onupdate for my widget.

我的问题是我找不到关于如何使用服务来监控更改的直接答案.我已经看到通过活动我可以将 android:configChanges="orientation|keyboardHidden" 添加到活动的清单并使用 onConfigurationChanged,但我可以这样做吗为一项服务.如果是这样怎么办?有没有更好的方法来监控方向变化?我还在互联网上读到,服务也不是做到这一点的最佳方式.

My problem is I can't find a straight answer as to how to use a service to monitor the change. I've seen that with an activity I can add android:configChanges="orientation|keyboardHidden" to the manifest for an activity and use a onConfigurationChanged, but can I do this for a service. If so how? Is there a better way to monitor the orientation change? I've also read on the internet that a service isn't the best way to do this either.

我看过有关创建方向侦听器的教程,但它们似乎使用了折旧的函数调用.

I've seen tutorials for creating orientation listeners but they seem to use depreciated function calls.

提前致谢

推荐答案

Service#onConfigurationChanged(Configuration newConfig) 对我有用.无需注册接收器以征求我的意见.我也没有向 AndroidManifest 添加任何过滤器.由于没有人提出此解决方案,我是否错过了讨论中的某些内容?我的服务正在前台运行.

Service#onConfigurationChanged(Configuration newConfig) works for me. No need to register Receivers for my opinion. I also did not add any filter to AndroidManifest. Did I miss something in the discussion since nobody suggested this solution? My Service is running on foreground.

只需放入您的服务中:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //Your handling
}

这篇关于我如何使用服务来监控 Android 中的方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆