0

I'm doing an app and I want give to the user the option to close the app automatically at the selected hour using the Timepicker. I knwo that i have to use the alarm manager, but i dont knw hot to combine with timepicker and close the app with it

             @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contadorpro);


    timePicker = (TimePicker) findViewById(R.id.timePicker);
    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Contador Activado", Toast.LENGTH_LONG).show();
            timePicker.clearFocus();
            final Calendar _calendar = Calendar.getInstance();
            final int hour = timePicker.getCurrentHour();
            final int minute = timePicker.getCurrentMinute();

            Thread _thread = new Thread() {

                @Override
                public void run() {

                    try {

                        Calendar _calendar = Calendar.getInstance();
                        int _currentHour = _calendar.get(Calendar.HOUR);
                        int _currentMinute = _calendar.get(Calendar.MINUTE);

                        if(_currentHour == hour && _currentMinute == minute){

                            System.exit(0);

                        }

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

            };

            _thread.start();

        }
    });



}   

}

David P
  • 3
  • 3

1 Answers1

0

You can get the time using

int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();

Then you must create a thread to check out the current time and the time from TimePicker.

At the time, you have to close the app using System.exit(0); ou finish() your activity

REEDITED:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contadorpro);

    timePicker = (TimePicker) findViewById(R.id.timePicker);
    button = (Button)findViewById(R.id.button);

    final Thread _thread = new Thread() {

        @Override
        public void run() {

            try {

                boolean _stopThread = true;

                while (_stopThread) {

                    Calendar _calendar = Calendar.getInstance();

                    int _currentHour = _calendar.get(Calendar.HOUR);
                    int _currentMinute = _calendar.get(Calendar.MINUTE);

                    int hour = timePicker.getCurrentHour();
                    int minute = timePicker.getCurrentMinute();

                    if (_currentHour == hour && _currentMinute == minute) {

                        _stopThread = false;

                        System.exit(0);

                    }

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    };

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // I edited here
            if(!_thread.isAlive()) {

                _thread.start();

                Toast.makeText(getApplicationContext(), "Contador Activado", Toast.LENGTH_LONG).show();

            }

        }
    });

}
Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
  • Thank you very much Lennon, it finally works ;) A colleague has told me a code to that the thread is not running all the time, when I get it and test it i will edit – David P Mar 12 '15 at 22:48
  • I'm glad to help you :D. So please put right in this answer for the people who are looking for something like that. ;) – Lennon Spirlandelli Mar 13 '15 at 11:47