我有簡單的提醒應用程序。我的應用程序的問題是,當我重新啓動手機或任何設備時,該應用程序開始拋出許多通知。 This是所有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();
}
}
}
你能不能幫我取消通知的裝置的重新啓動後。在代碼中告訴在特定時間安排通知,但我很困惑爲什麼設備重新啓動後,我從應用程序獲得許多通知。有人能告訴我哪裏錯了嗎?
更新:
OnBootReceiver:
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);
}
後reminderMgr.setReminder功能以及 – Tarun
更新的問題.. – AndroidProgrammer
檢查你的條目是否從返回數據庫是否過去?您只提取時間大於當前時間的條目。 – Tarun