Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
当myTime与android系统时间匹配时,如何启动活动?_Android_Android Intent_Android Activity_Android Alarms_Android Calendar - Fatal编程技术网

当myTime与android系统时间匹配时,如何启动活动?

当myTime与android系统时间匹配时,如何启动活动?,android,android-intent,android-activity,android-alarms,android-calendar,Android,Android Intent,Android Activity,Android Alarms,Android Calendar,我正在努力完成一项任务。下面是正在发生的事情。我首先将共享首选项中的一个值保存在来自第一个活动的字符串中,并将该值传递给下面的此活动。然后在我的第二个活动中,我称之为值。然后我获取android的当前时间并将其转换为字符串 ===================================================== @懒惰忍者:更新请检查:好的。我终于得到了比赛的时间!现在还有最后一件事,我如何在后台运行这个“if语句”,这样即使应用程序关闭,系统也会不断检查这两个值 =====

我正在努力完成一项任务。下面是正在发生的事情。我首先将共享首选项中的一个值保存在来自第一个活动的字符串中,并将该值传递给下面的此活动。然后在我的第二个活动中,我称之为值。然后我获取android的当前时间并将其转换为字符串

=====================================================

@懒惰忍者:更新请检查:好的。我终于得到了比赛的时间!现在还有最后一件事,我如何在后台运行这个“if语句”,这样即使应用程序关闭,系统也会不断检查这两个值

=====================================================

更新代码:

                Intent in = new Intent(Alarm.this, FajrAlarmRecieverActivity.class);
                PendingIntent pi = PendingIntent.getActivity(Alarm.this, 2, in, PendingIntent.FLAG_CANCEL_CURRENT);

                /*My Time from Shared Preference*/
                //SharedPreferences prfs = getSharedPreferences("customeAlarmTimes", Context.MODE_PRIVATE);
                //myTime = prfs.getString("Isha", "Isha");

                String myTime = "11:15";

                SimpleDateFormat formatter = new SimpleDateFormat("K:mm"); 
                Date date = formatter.parse(myTime); // You will need try/catch around this
                long millis = date.getTime();
                /*My Time from Shared Preference*/

                /*System Time*/
                Date today = Calendar.getInstance().getTime();
                String reportDate = new SimpleDateFormat("K:mm").format(today);
                Date swag = formatter.parse(reportDate);
                long currentTime= swag.getTime();

                Log.d("SystemTime", reportDate);
                Log.d("MyTime", myTime);
                Log.d("SystemTime in Millis", currentTime+"");
                Log.d("MyTime in Millis", millis+"");

                if(millis == currentTime){
                    SendNotification();
                    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, pi);
                }
                else if (millis > currentTime){
                    Toast mtoast = Toast.makeText(getApplicationContext(), "FAJR TIME > CURRENT TIME", Toast.LENGTH_LONG);
                    mtoast.show();
                }
                else if (millis < currentTime){
                    Toast mtoast = Toast.makeText(getApplicationContext(), "FAJR TIME < CURRENT TIME", Toast.LENGTH_LONG);
                    mtoast.show();
                }
    }
}
Intent in=new Intent(Alarm.this,fajRarmReceiveActivity.class);
PendingEvent pi=PendingEvent.getActivity(Alarm.this,2,in,PendingEvent.FLAG_CANCEL_CURRENT);
/*我的时间来自共同的偏好*/
//SharedReferences prfs=GetSharedReferences(“customeAlarmTimes”,Context.MODE\u PRIVATE);
//myTime=prfs.getString(“Isha”、“Isha”);
字符串myTime=“11:15”;
SimpleDataFormat格式化程序=新的SimpleDataFormat(“K:mm”);
日期日期=格式化程序。解析(myTime);//你需要设法解决这个问题
long millis=date.getTime();
/*我的时间来自共同的偏好*/
/*系统时间*/
Date today=Calendar.getInstance().getTime();
String reportDate=新的SimpleDateFormat(“K:mm”).format(今天);
dateswag=formatter.parse(reportDate);
long currentTime=swag.getTime();
Log.d(“系统时间”,报告日期);
Log.d(“我的时间”,我的时间);
Log.d(“系统时间单位为毫秒”,currentTime+”);
Log.d(“我的时间单位为毫秒”,毫秒+”);
如果(毫秒==当前时间){
发送通知();
AlarmManager am=(AlarmManager)getSystemService(Context.ALARM\u服务);
am.set(AlarmManager.RTC_唤醒,AlarmManager.INTERVAL_DAY,pi);
}
否则如果(毫秒>当前时间){
Toast mtoast=Toast.makeText(getApplicationContext(),“FAJR TIME>CURRENT TIME”,Toast.LENGTH\u LONG);
mtoast.show();
}
else if(毫秒<当前时间){
Toast mtoast=Toast.makeText(getApplicationContext(),“FAJR时间<当前时间”,Toast.LENGTH\u LONG);
mtoast.show();
}
}
}

使用重复报警。它将为您处理这些条件
试试下面的方法

Intent i = new Intent(getApplicationContext(), FajrAlarmRecieverActivity.class); 
PendingIntent sender = PendingIntent.getActivity(Alarm.this, 0, i, 0);

// Get Time from your sharedpreferences
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour); // set Fajr hour
calendar.set(Calendar.MINUTE, minute); // set Fajr minute
calendar.set(Calendar.SECOND, 0); // set seconds  ( well no need for seconds)    
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 
别忘了在清单文件中添加FajRarmReceiveActivity。
阅读您的评论后,只需使用set。
警报触发后,读取SharedReferences和,然后再次设置警报。
编辑 获取当前时间

long currentTime= System.currentTimeMillis();
从SharedPref中获取时间,并将其转换为毫秒

if( sharedTimeInMillis < currentTime ){
   // your good stuff
}
myTime = prfs.getString("Isha", "Isha");
SimpleDateFormat formatter = new SimpleDateFormat("K:mm"); 
Date date = formatter.parse(myTime ); // You will need try/catch around this
long millis = date.getTime();

所以,如果你想在下午3点点火,而且已经是下午4点了,那么应该在明天下午4点点火?不,如果我想在下午3点点火,并且已经是下午4点了,那么它应该重新安排在明天下午3点点火是的,对不起,我的意思是;)是的,当系统时间与保存的首选项值匹配时,也会触发警报。:)我试过了。。。只在特定时间触发警报。我不想那样。我希望它与共享首选项中保存的值匹配。如果保存的值与android时间匹配,则应触发警报。假设我从首选项中保存的值是:10:54。。。所以我想做的是继续检查android的时间和android的时间匹配的时刻,这次是10:54。关掉警报器。:)您是否一直在更新共享优先级值?共享优先级值每24小时更新一次。:)@懒鬼:现在我想问你的是如何比较这两个时间,共享偏好值和来自android的时间。我喜欢这样:如果(myTime==reportDate){///这里有好东西//};唯一的问题是它只检查一次:(这意味着它不会继续进行。我现在要做的就是比较这两次,并让它们一直检查,直到它们进行比较。我希望这发生在后台,这样即使应用程序关闭,此活动仍然可以检查和比较时间,并触发其中的警报。那么是否有人能够引导我进入该目录关于我将如何将myTime值与本地android时间进行比较。当它匹配时,它会发出警报。无论应用程序是打开还是关闭的。:)