使用开关取消警报

时间:2017-03-18 15:17:32

标签: android sql adapter alarmmanager alarm

您好我正在尝试使用交换机禁用和警报,并且当我尝试禁用它时,从SQL数据库获取特定的警报ID,当前我正在接收空引用。我现在还在努力,但是如果有人也可以快速看一下那会很棒。

在列表视图适配器上创建警报

final int id = (int) System.currentTimeMillis();

pendingIntent = PendingIntent.getBroadcast(v.getContext(), id, receiver, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);

intentArrayList.add(pendingIntent);

我尝试从BaseAdapter取消数据库。

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

cursor.moveToPosition(position);

if (mySwitch.isChecked()){

Toast.makeText(view.getContext(), Integer.toString(position) + "ENABLED", Toast.LENGTH_SHORT).show();

} else {

String id = cursor.getString(cursor.getColumnIndexOrThrow("alarm_code"));

pendingIntent = PendingIntent.getBroadcast(view.getContext(),   Integer.valueOf(id), receiver, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.cancel(pendingIntent);

Toast.makeText(view.getContext(), "DISABLED", Toast.LENGTH_SHORT).show();

            }

当前错误消息.....

尝试在空对象引用上调用虚方法'void android.content.Intent.prepareToLeaveProcess(android.content.Context)'

1 个答案:

答案 0 :(得分:0)

如果您不忘记将这两行放入

,修正了它的错误
alarmManager = (AlarmManager) view.getContext().getSystemService(Context.ALARM_SERVICE);
receiver = new Intent(view.getContext(), Alarm_Reciever.class);