android - 服务可以包含 Activity 吗?

标签 android android-service

我想创建一个具有 onWindowFocusChanged 方法的服务,该方法是 Activity 的原生方法。但是我们的客户要求平板电脑不断地寻找这样的焦点变化,有没有办法将这样的监听器添加到服务中或让 Activity 作为服务的一部分运行?

最佳答案

服务没有 onWindowFocusChanged() 方法。

但是您可以在作为所有 Activity 父级的基本 Activity 中覆盖 onWindowFocusChanged(),并通过向其发送 Intent 来通知您的服务:

Intent intent = new Intent("focus changed");
intent.setClass(this, MyService.class);
startService(intent);

您将通过覆盖 onStartCommand(Intent intent, int flags, int startId) 方法在服务中接收 Intent 。

关于android - 服务可以包含 Activity 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13033124/

相关文章:

android - Google APIs - Android BigQuery 客户端的应用级授权

android - Retrofit - 我可以映射响应数据字段吗?

java - 无法停止android后台服务

android - 将 LocationManager 作为 Android 服务启动

android - 我对 Android 中 Activity 、服务和广播之间的主要区别是否正确?

android - 在android应用程序的后台服务中强制关闭

java - 如何在android中实现手绘图片裁剪?

android - iOS SDK 中 NSURLProtocol 的 Android 等价物是什么?

android - 关闭应用程序时如何停止Android服务

java - 多次循环一个方法会导致错误