无法将ReceiverRestrictedContext强制转换为android.app.Activity [英] ReceiverRestrictedContext cannot be cast to android.app.Activity

查看:558
本文介绍了无法将ReceiverRestrictedContext强制转换为android.app.Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BroadcastReceiver中需要一个Activity,但是当我尝试将上下文强制转换为activity时,它将无法正常工作.

I need an Activity in my BroadcastReceiver, but when i try to cast the context to activity, it doesn't work.

原因:java.lang.ClassCastException:android.app.ReceiverRestrictedContext无法转换为android.app.Activity

Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity

如何从BroadcastReceiver获取活动?

How can I get Activity from BroadcastReceiver ?

谢谢

推荐答案

如何从BroadcastReceiver获取活动?

How can I get Activity from BroadcastReceiver ?

通常,您不能. BroadcastReceiver独立于活动.例如,您可以让BroadcastReceiver在没有Activity运行时接收广播.

Generally, you can't. A BroadcastReceiver is independent from an Activity. For example, you can have BroadcastReceiver which receive broadcasts when no Activity is running.

但是,您可以在Activity中注册/注销BroadcastReceiver,并在其运行时使用它来接收广播.

You can, however, register/unregister a BroadcastReceiver in an Activity and use it to receive broadcasts while your Activity is running.

这篇关于无法将ReceiverRestrictedContext强制转换为android.app.Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆