0

我正在尝试从应用程序小部件启动/停止服务,同样我还想更改服务启动/停止小部件图像。

我制作了小部件,它显示在主屏幕上。我可以从小部件调用一个活动,但是当我尝试启动服务时,我收到了这个错误:

?:??: W/?(?): FATAL EXCEPTION: main
?:??: W/?(?): java.lang.RuntimeException: Unable to start service com.gagan.smart.lock.SmartLockWidgetService@2b84ec78 with Intent { flg=0x10000000 cmp=com.gagan.smart.lock/.SmartLockWidgetService bnds=[141,416][219,494] }: java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
?:??: W/?(?):   at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2633)
?:??: W/?(?):   at android.app.ActivityThread.access$1900(ActivityThread.java:142)
?:??: W/?(?):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
?:??: W/?(?):   at android.os.Handler.dispatchMessage(Handler.java:99)
?:??: W/?(?):   at android.os.Looper.loop(Looper.java:137)
?:??: W/?(?):   at android.app.ActivityThread.main(ActivityThread.java:4931)
?:??: W/?(?):   at java.lang.reflect.Method.invokeNative(Native Method)
?:??: W/?(?):   at java.lang.reflect.Method.invoke(Method.java:511)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
?:??: W/?(?):   at dalvik.system.NativeStart.main(Native Method)
?:??: W/?(?): Caused by: java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
?:??: W/?(?):   at com.gagan.smart.lock.SmartLockWidgetService.onStartCommand(SmartLockWidgetService.java:25)
?:??: W/?(?):   at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2616)
?:??: W/?(?):   ... 10 more

我在 manifest.xml 中添加了服务名称和小部件

请帮助我如何从小部件启动/停止服务,同时在小部件单击时更改停止/停止小部件按钮图像。

谢谢

4

0 回答 0