简体   繁体   中英

AlarmManger with show notification on specific time

I have the following code (abbreviated to the relevant part). If I set the alarm manger on a specific time and when this time comes the notification doesn't show up. I want to set date like sun-mon-tue.

Code Snippet :

public class service extends Service {

    private int Period_You_Want_in_MilliSecond = 30 * 1000;
    Calendar calender ;
    SimpleDateFormat format ;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        DataBase DB=new DataBase(this);
        //get row from database where id=1
        final CourseClass C=DB.getCousre(1);

        calender = Calendar.getInstance();
        format = new SimpleDateFormat("hh:mm aa");

        Toast.makeText(getApplicationContext(), "service entered", Toast.LENGTH_LONG).show();

        final Handler h = new Handler();
        Runnable r = new Runnable() {

            @Override
            public void run() {


                String current_time = format.format(calender.getTime());
                //Toast.makeText(getApplicationContext(), ""+current_time,Toast.LENGTH_LONG).show();

                //check if the row reterned if filed name has data lenght>0 return True
                if(C.getName().length()>0){
                    if(current_time.equals("01:24 PM")){
                        //Toast.makeText(getApplicationContext(), "TIMER",Toast.LENGTH_LONG).show();

                        show_notification();

                    }
                }
                h.postDelayed(this,Period_You_Want_in_MilliSecond);
            }
        };      


        h.post(r);

        return super.onStartCommand(intent, flags, startId);
    }
}

You are not using the AlarmManager, you are using a Handler. Take a look at this example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM