开发者

Android: Using methods from an Activity in a Widget. Extend Activity and AppWidgetProvider?

开发者 https://www.devze.com 2023-02-09 00:42 出处:网络
I\'m working on an Android app 开发者_如何学Gowhich has an activity and a widget.This is currently implemented via two classes in two .java files - one for the activity (extending Activity), one for t

I'm working on an Android app 开发者_如何学Gowhich has an activity and a widget. This is currently implemented via two classes in two .java files - one for the activity (extending Activity), one for the widget (extending AppWidgetProvider). Nothing out of the ordinary here as far as I'm aware...

However, the widget class code could be a lot simpler if it was to make use of functions and asynctasks defined in the activity class. Duplicating these functions seems like bad design, so I'm wondering how I can structure the app to make them usable?

Can I extend both Activity and AppWidgetProvider somehow? Can I import one in the other?

Thanks!


either make the funcs static, or make a 3rd class to hold these funcs


Move the functions down into a service. Create a Service and you can use context.startService(Intent) from you WigetProvider or from the activity to access the functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号