我有简单的提醒应用程序。我的应用程序的问题是,当我重新启动手机或任何设备时,应用程序开始抛出许多通知。这是所有 Java 文件的源代码。这是 OnBootReceiver :
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = ComponentInfo.class.getCanonicalName();
@Override
public void onReceive(Context context, Intent intent) {
ReminderManager reminderMgr = new ReminderManager(context);
RemindersDbAdapter dbHelper = new RemindersDbAdapter(context);
dbHelper.open();
Cursor cursor = dbHelper.fetchAllReminders();
if(cursor != null) {
cursor.moveToFirst();
int rowIdColumnIndex = cursor.getColumnIndex(RemindersDbAdapter.KEY_ROWID);
int dateTimeColumnIndex = cursor.getColumnIndex(RemindersDbAdapter.KEY_DATE_TIME);
while(cursor.isAfterLast() == false) {
Log.d(TAG, "Adding alarm from boot.");
Log.d(TAG, "Row Id Column Index - " + rowIdColumnIndex);
Log.d(TAG, "Date Time Column Index - " + dateTimeColumnIndex);
Long rowId = cursor.getLong(rowIdColumnIndex);
String dateTime = cursor.getString(dateTimeColumnIndex);
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(ReminderEditActivity.DATE_TIME_FORMAT);
try {
java.util.Date date = format.parse(dateTime);
cal.setTime(date);
reminderMgr.setReminder(rowId, cal);
} catch (java.text.ParseException e) {
Log.e("OnBootReceiver", e.getMessage(), e);
}
cursor.moveToNext();
}
cursor.close() ;
}
dbHelper.close();
}
}
这是 WakeReminderIntentService:
public abstract class WakeReminderIntentService extends IntentService {
abstract void doReminderWork(Intent intent);
public static final String LOCK_NAME_STATIC="com.dummies.android.taskreminder.Static";
private static PowerManager.WakeLock lockStatic=null;
public static void acquireStaticLock(Context context) {
getLock(context).acquire();
}
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic==null) {
PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE);
lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME_STATIC);
lockStatic.setReferenceCounted(true);
}
return(lockStatic);
}
public WakeReminderIntentService(String name) {
super(name);
}
@Override
final protected void onHandleIntent(Intent intent) {
try {
doReminderWork(intent);
}
finally {
//getLock(this).release();
}
}
}
你能帮我在设备重启后取消通知吗?在代码中,被告知要在特定时间安排通知,但我很困惑为什么在设备重启后我会从应用程序收到很多通知。有人可以告诉我错误在哪里吗?
更新:
OnBoot接收器:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ComponentInfo;
import android.database.Cursor;
import android.util.Log;
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = ComponentInfo.class.getCanonicalName();
@Override
public void onReceive(Context context, Intent intent) {
ReminderManager reminderMgr = new ReminderManager(context);
RemindersDbAdapter dbHelper = new RemindersDbAdapter(context);
dbHelper.open();
Cursor cursor = dbHelper.fetchAllReminders();
if(cursor != null) {
cursor.moveToFirst();
int rowIdColumnIndex = cursor.getColumnIndex(RemindersDbAdapter.KEY_ROWID);
int dateTimeColumnIndex = cursor.getColumnIndex(RemindersDbAdapter.KEY_DATE_TIME);
while(cursor.isAfterLast() == false) {
Log.d(TAG, "Adding alarm from boot.");
Log.d(TAG, "Row Id Column Index - " + rowIdColumnIndex);
Log.d(TAG, "Date Time Column Index - " + dateTimeColumnIndex);
Long rowId = cursor.getLong(rowIdColumnIndex);
String dateTime = cursor.getString(dateTimeColumnIndex);
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(ReminderEditActivity.DATE_TIME_FORMAT);
try {
java.util.Date date = format.parse(dateTime);
cal.setTime(date);
reminderMgr.setReminder(rowId, cal);
} catch (java.text.ParseException e) {
Log.e("OnBootReceiver", e.getMessage(), e);
}
cursor.moveToNext();
}
cursor.close() ;
}
dbHelper.close();
}
}
setReminder().方法:
public void setReminder(Long taskId, Calendar when) {
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}