2

我有简单的提醒应用程序。我的应用程序的问题是,当我重新启动手机或任何设备时,应用程序开始抛出许多通知。是所有 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);
}
4

2 回答 2

0

Okei 我在你的源代码中查找过,我认为你应该在你的清单中取消这一行:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
于 2013-08-23T18:48:26.093 回答
0

在类中的saveState函数中ReminderEditActivity不要将时间保存为格式化日期。相反,只需保存calendar.getTimeInMillis().

示例

private void saveState() {
    long id = mDbHelper.createReminder(title, body, mCalendar.getTimeInMillis());
}

现在在您的 fetchAllReminder 查询中,您可以添加:

示例

Calendar currentTime = Calendar.getInstance();
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_DATE_TIME}, null, "reminder_date_time > " + currentTime.getTimeInMillis(), null, null, null); 
于 2013-08-24T07:54:40.123 回答