Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何在后台检查Android是否达到了特定的日期和时间_Android_Date_Time_Background_Sharedpreferences - Fatal编程技术网

我如何在后台检查Android是否达到了特定的日期和时间

我如何在后台检查Android是否达到了特定的日期和时间,android,date,time,background,sharedpreferences,Android,Date,Time,Background,Sharedpreferences,我正在尝试制作一个程序,在后台检查是否达到了特定的日期和时间。我想在SharedReference中保存日期和时间。SharedReference不是问题所在,但我需要知道如何创建在后台运行的服务,并检查SharedReference中的日期和时间是否等于当前时刻的日期和时间。我还需要在开机时启动服务。这就是我现在拥有的: Android清单 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"

我正在尝试制作一个程序,在后台检查是否达到了特定的日期和时间。我想在SharedReference中保存日期和时间。SharedReference不是问题所在,但我需要知道如何创建在后台运行的服务,并检查SharedReference中的日期和时间是否等于当前时刻的日期和时间。我还需要在开机时启动服务。这就是我现在拥有的:

Android清单

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name="Receiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name=".Service"
        android:exported="false"/>
服务

SharedPreferences prefs;

public Service() {
    super("Service");
}

@Override
protected void onHandleIntent(Intent workIntent) {
    prefs = getSharedPreferences("nl.martijnvk.declareerassistent", Context.MODE_PRIVATE);

}
编辑:


多亏了Prashanth,我让它与AlarmManager类一起工作,但是毫秒的计算是不正确的。在我的日志中,他打印了146963215229的值,但我的日历设置为一分钟后。有什么问题吗?

正如你提到的,你知道如何使用共享偏好

  • 然后可以从SharedReference中以字符串形式读取日期
  • 然后,您需要在日期对象中转换该字符串:

    公共静态日期getDateFromString(字符串dateString,字符串格式){ 日期=空; 试一试{ if(dateString!=null){ SimpleDataFormat dateFormat=新的SimpleDataFormat(格式); date=dateFormat.parse(日期字符串); } } 捕获(例外e){ } 返回日期; }

  • 注意:这里的字符串格式可以是您保存在SharedRef中的任何模式。

  • 获取当前日期

    日期currentDate=新日期()

  • 然后,您可以使用以下任何方法比较这两个日期

    公共静态布尔值IseQualifierFore(Date baseDate、Date dateToCompare){

  • 在你的活动中

      Calendar calendar = Calendar.getInstance();
    
      calendar.set(Calendar.MONTH, 6);
      calendar.set(Calendar.YEAR, 2013);
      calendar.set(Calendar.DAY_OF_MONTH, 13);
    
      calendar.set(Calendar.HOUR_OF_DAY, 20);
      calendar.set(Calendar.MINUTE, 48);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.AM_PM,Calendar.PM);
    
      Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
    
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    
    创建将被触发的广播接收器

    {
    
    @Override
    public void onReceive(Context context, Intent intent)
    {
       // it is triggered on the time you scheduled 
    
    }   
    

    }

    签出报警管理器类嗨,您将日历设置为什么。我不明白你的意思。就几毫秒?应该很好用。可能是android操作系统在拖延。对于阈值,您可以在几毫秒之前安排。您可以发布设置日历的代码吗?因此,我使用第一个代码设置接收器,并且在到达日期时触发接收器?然后在OnReceive中,我必须在到达日期时输入要执行的代码?或者我必须在onReceive中插入比较代码吗?无需再次检查,因为您正在为您的时间安排活动。我尝试了这一点,在onReceive事件中干杯,但当Day的时间到达日历时间时,什么也没有发生。这回答了如何调整日期的问题,但它并没有回答如何在后台继续检查的问题。您可以编写一个计时器任务,在服务中的某个预定义时间间隔后执行上述代码。
      Calendar calendar = Calendar.getInstance();
    
      calendar.set(Calendar.MONTH, 6);
      calendar.set(Calendar.YEAR, 2013);
      calendar.set(Calendar.DAY_OF_MONTH, 13);
    
      calendar.set(Calendar.HOUR_OF_DAY, 20);
      calendar.set(Calendar.MINUTE, 48);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.AM_PM,Calendar.PM);
    
      Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
    
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    
    {
    
    @Override
    public void onReceive(Context context, Intent intent)
    {
       // it is triggered on the time you scheduled 
    
    }