当前位置: 动力学知识库 > 问答 > 编程问答 >

date and time notification not firing in android

问题描述:

Hi in my application i designed application with date and time i want to fire the notification on particular time and date.

Now My problem if i changed the system date also notification firing i don't want like that based on current date and system date i want to fire the notification.

Can anyone please help me how to solve the issue

MyView class

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);

}

}

Thanks in adavance.

Updated code

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);*/

}

}

网友答案:

You can use the server time or your network provider time.. below is the code to get the time from your network provider..

LocationManager locationManager = (LocationManager)activity.getSystemService(activity.LOCATION_SERVICE);

long networkTimeOnRequest = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();

And now, networkTimeOnRequest will hold the exact network time and you use this to compare with the date/time you specify.

网友答案:
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.        

And set timer like this

new CountDownTimer(millisecondsFromNow , 1000) {

     public void onTick(long millisUntilFinished) {

     }

     public void onFinish() {
        //It's your date. Do your stuff here.
     }
  }.start();
分享给朋友:
您可能感兴趣的文章:
随机阅读: