我想在7天后發佈數據重複。爲此,我使用Alarm Manager的setRepeat方法。當手機重新啓動時,鬧鐘將被清除。我使用啓動接收器並重新設置鬧鐘。在nadroid重新啓動時清除報警
但是如果手機在第6天重新啓動並且鬧鐘再次設置,則鬧鐘將在第6 + 7天響鈴,而不是在第7天應該響起。 有沒有辦法解決這個問題?
對不起,我英文很差。 請幫助
我想在7天后發佈數據重複。爲此,我使用Alarm Manager的setRepeat方法。當手機重新啓動時,鬧鐘將被清除。我使用啓動接收器並重新設置鬧鐘。在nadroid重新啓動時清除報警
但是如果手機在第6天重新啓動並且鬧鐘再次設置,則鬧鐘將在第6 + 7天響鈴,而不是在第7天應該響起。 有沒有辦法解決這個問題?
對不起,我英文很差。 請幫助
當設置鬧鐘時,將觸發時間保存在共享偏好設置中。並且在啓動接收器上再次設置鬧鐘,但在這種情況下,觸發時間來自共享偏好設置。
在AndroidManifest.xml中
<receiver android:name="com.Company.PackageName.BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
在你com.Company.PackageName.BootCompletedReceiver類
public class BootCompletedReceiver extends BroadcastReceiver
{
private Context mContext;
private G9SharedPreferences oSharedPreferences;
private AlarmManager amSchedules;
@Override
public void onReceive(Context context, Intent intent)
{
this.mContext = context;
oSharedPreferences = new G9SharedPreferences(this.mContext);
try
{
vUpdateAlarmManager();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
oSharedPreferences = null;
this.mContext = null;
}
}
@SuppressWarnings("deprecation")
public void vUpdateAlarmManager()
{
vCancelAlaramManager();
Date dCurrentDate = new Date(oSharedPreferences.GetLongPreferences(G9Constant.BACKUP_TIME, 0));
if(dCurrentDate.getTime() < new Date().getTime())
{
dCurrentDate.setDate(dCurrentDate.getDate() + 1);
}
vSetRepeatingAlarm(dCurrentDate);
}
public void vCancelAlaramManager()
{
Intent oIntent = new Intent(mContext, AlarmReceiver.class);
PendingIntent oPendingIntent = PendingIntent.getBroadcast(mContext, 0, oIntent, 0);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(oPendingIntent);
oPendingIntent.cancel();
}
public void vSetRepeatingAlarm(Date dScheduleTime)
{
amSchedules = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
amSchedules.setRepeating(AlarmManager.RTC_WAKEUP, dScheduleTime.getTime(), AlarmManager.INTERVAL_DAY , pendingIntent);
}
}
SharedPreferences可以幫助你將數據存儲在磁盤上,並在以後讀(如重新啓動後) – fdomig 2012-08-02 07:57:36