java - Android - 为类设置 View ,而不是扩展 Activity

标签 java android android-intent service view

如何为不扩展 Activity 的类创建和设置 View ?

我的“MyServiceIntent”类扩展了 IntentService,而不是 Activity。

如何更改 MyServiceIntent 类中的当前 View ?目前它仅显示 Main 的 View 。但我需要在 MyServiceIntent 类中显示一个新 View 。

我尝试过:

setContentView(R.layout.my_layout) 

但我不能调用它,因为我的“MyServiceIntent”类没有扩展 Activity。

我还尝试了静态“MyServiceIntent”类,但它无法从主类访问 View 数据。

谢谢

最佳答案

你不能。

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

https://developer.android.com/guide/components/services.html

但是,您仍然可以从服务更改正在运行的 Activity 的 View !为此,您应该绑定(bind) Activity 和服务。请参阅here一些示例代码

关于java - Android - 为类设置 View ,而不是扩展 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40226537/

相关文章:

java - jmeter调用 Controller 中的其他线程组变量

android - 我应该在获得 Intent 后清除 bundle getExtra 吗?

Android - 将 Intent.ACTION_GET_CONTENT 限制为特定的文件扩展名

c# - 等待 RunOnUIThread 完成并继续执行剩余的任务

java - 实现一个扩展 Parcelable 的接口(interface)

android - 任务 ':app:kaptDebugKotlin' 的 java.lang.reflect.InvocationTargetException(无错误消息)

android - 当屏幕方向改变时,相机 Activity 导致 uri 变为空

java - 如何使用java代码设置html文本字段的值

Java:我不明白这个 Sprite 表读取代码的部分意义是什么?

java - 为什么文本节点值之间没有空格?