[英]Android - Set view for a class, not extending Activity
How can I create and set a View for a class that doesnt extend activity? 如何为不扩展活动的类创建和设置视图?
My 'MyServiceIntent' class extends IntentService, rather than Activity. 我的“ MyServiceIntent”类扩展了IntentService,而不是Activity。
How can I change the current view in the MyServiceIntent class? 如何在MyServiceIntent类中更改当前视图? Currently it just shows Main's view.
目前,它仅显示Main的视图。 But I need a new view shown in my MyServiceIntent class.
但是我需要MyServiceIntent类中显示的新视图。
I tried: 我试过了:
setContentView(R.layout.my_layout)
but I cant call it as my 'MyServiceIntent' class doesn't extend Activity. 但我无法调用它,因为我的“ MyServiceIntent”类没有扩展Activity。
I also tried static 'MyServiceIntent' class, but it wasn't able to access the view data from the main class. 我还尝试了静态“ MyServiceIntent”类,但它无法从主类访问视图数据。
Thanks 谢谢
You cannot. 你不能。
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 https://developer.android.com/guide/components/services.html
However, you can still alter the view of a running activity from the service! 但是,您仍然可以从服务中更改正在运行的活动的视图! For this you should bind the activity and the service.
为此,您应该绑定活动和服务。 See here for some example code
看到这里一些示例代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.