【发布时间】:2016-12-09 13:10:40
【问题描述】:
我有一个这样的应用程序类:
public class MyApplication extends Application {
}
在Manifest中注册:
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
</application>
我使用这个 Application 类来保存 Dagger 组件等。
现在我有一个广播接收器:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent){
MyApplication myApplication = (MyApplication) context.getApplicationContext();
}
}
它在 Manifest 中注册为InstallReferrerReceiver:
<receiver
android:name="my.package.MyReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER"/>
</intent-filter>
</receiver>
如您所见,我将应用程序上下文转换为我的应用程序类,它在活动等中运行良好,通常也在这里。
虽然我收到异常,但通过 Crashlytics:
Unable to start receiver my.package.BroadcastReceiver: java.lang.ClassCastException: android.app.Application cannot be cast to my.package.MyApplication
我的问题是:我是否不保证接收我的应用程序对象作为广播接收器中的应用程序上下文?
【问题讨论】:
-
应该可以的。报告此崩溃的设备之间是否有任何共性?此外,
<receiver>元素(例如,您使用的是android:process)或广播的发送方式是否有任何异常? -
我认为不能保证您可以在
BroadcastReceiver中访问您的应用程序(我正在寻找源代码)。你能查一下if (context.getApplicationContext() instanceof MyApplication)吗? -
@CommonsWare 抱歉,这可能很重要,我已经为接收者添加了清单条目。
-
@ThomasRoulin 是的,我当然可以检查,但最终我需要以某种方式保证拥有 my 应用程序,否则我必须解决这个问题。跨度>
-
<receiver>看起来又好又无聊。 :-) AFAIK,你应该得到一个MyApplication。自己测试一下(例如,使用adb shell am发送匹配的广播),看看它是否有效。如果确实如此,请再次查找报告此崩溃的设备的共性。
标签: android broadcastreceiver android-context