我正在尝试使用警报管理器从我的应用程序的 sqlite 数据库中获取一些值。现在我们都知道扩展 SQLiteOpenHelper 的 DB 类需要一个上下文,并且广播接收器得到一个这样的上下文:
@Override
public void onReceive(Context context, Intent intent) {
问题是,当我使用此上下文调用 DB 类时,出现以下异常:
无法启动接收器 com.ayham.mycar.AlarmBroadcastReceiver:java.lang.ClassCastException:android.app.ReceiverRestrictedContext
完整的代码是这样的:
public class AlarmBroadcastReceiver extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
//get the values from the DB:
DBhelper mDBGet=new DBhelper(context);
Cursor mCursor=mDBGet.allProperties((Activity) context);}}
我知道我不应该将上下文转换为 Activity 但如果我不这样做,编译器会给我一个错误。我怎样才能完成这项工作?