如何为不扩展 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/