您好,在我的应用程序中,我设计了具有日期和时间的应用程序,我想在特定时间和日期发出通知。
现在我的问题是,如果我更改了系统日期,也会根据当前日期和系统日期发出通知,我想要触发通知。
任何人都可以帮我解决问题
MyView类
public class MyView extends Activity {
TextView tv;
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Calendar Calendar_Object = Calendar.getInstance();
Calendar firingCal = Calendar.getInstance();
firingCal.set(Calendar.DAY_OF_MONTH, 10);
firingCal.set(Calendar.MONTH, 7);
firingCal.set(Calendar.YEAR, 14);
Calendar firingCal1 = Calendar.getInstance();
firingCal1.set(Calendar.HOUR_OF_DAY, 15);
firingCal1.set(Calendar.MINUTE, 55);
firingCal1.set(Calendar.SECOND, 0);
// MyView is my current Activity, and AlarmReceiver is the
// BoradCastReceiver
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
/*
* The following sets the Alarm in the specific time by getting the long
* value of the alarm date time which is in calendar object by calling
* the getTimeInMillis(). Since Alarm supports only long value , we're
* using this method.
*/
alarmManager.set(AlarmManager.RTC_WAKEUP, firingCal.getTimeInMillis(),
pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, firingCal1.getTimeInMillis(),
pendingIntent);
}
}
谢谢你。
更新了代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Calendar Calendar_Object = Calendar.getInstance();
/* Calendar firingCal = Calendar.getInstance();
firingCal.set(Calendar.DAY_OF_MONTH, 10);
firingCal.set(Calendar.MONTH, 7);
firingCal.set(Calendar.YEAR, 14);
Calendar firingCal1 = Calendar.getInstance();
firingCal1.set(Calendar.HOUR_OF_DAY, 23);
firingCal1.set(Calendar.MINUTE, 55);
firingCal1.set(Calendar.SECOND, 0);
*/
String input = "Sat Feb 17 2015";
Date date = null;
try {
date = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH).parse(input);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //What ever might be your format
long milliseconds = date.getTime();
long millisecondsFromNow = milliseconds - (new Date()).getTime();
new CountDownTimer(millisecondsFromNow , 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//It's your date. Do your stuff here.
}
}.start();
// MyView is my current Activity, and AlarmReceiver is the
// BoradCastReceiver
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
/*
* The following sets the Alarm in the specific time by getting the long
* value of the alarm date time which is in calendar object by calling
* the getTimeInMillis(). Since Alarm supports only long value , we're
* using this method.
*/
/*alarmManager.set(AlarmManager.RTC_WAKEUP, firingCal.getTimeInMillis(),
pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, firingCal1.getTimeInMillis(),
pendingIntent);*/
}
}
答案 0 :(得分:0)
您可以使用服务器时间或网络提供商时间..以下是从网络提供商处获取时间的代码..
LocationManager locationManager = (LocationManager)activity.getSystemService(activity.LOCATION_SERVICE);
long networkTimeOnRequest = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
现在,networkTimeOnRequest将保留确切的网络时间,您可以使用它来与您指定的日期/时间进行比较。
答案 1 :(得分:0)
String input = "Sat Feb 17 2015";
Date date = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH).parse(input); //What ever might be your format
long milliseconds = date.getTime();
long millisecondsFromNow = milliseconds - (new Date()).getTime();
//Instead of new Date() you can get your server time here.
并像这样设置计时器
new CountDownTimer(millisecondsFromNow , 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//It's your date. Do your stuff here.
}
}.start();