-2

Hello I am new to android and java I need help !! In this code i am traying to keep the countdown working after destroying or stopping the application (android) i want the phone to lock even if i stop the application you can see the code below i know i need onStop(); method but i don't know what to type inside it thanks by advance

public class SetTimeActivity extends AppCompatActivity {
    Button set_btn ;
    int min  ;
    EditText setTimerMenu_et;
    TextView tv_display ;
    DevicePolicyManager devicePolicyManager;
    ComponentName componentName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_time);
        set_btn = (Button)findViewById(R.id.set_btn);
        setTimerMenu_et = (EditText)findViewById(R.id.setTimerMenu_et);

    tv_display = (TextView)findViewById(R.id.tv_display);
    devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    componentName = new ComponentName(SetTimeActivity.this , Controller.class);

    //-------------------------Clicking SET button---------------------------------
    set_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = setTimerMenu_et.getText().toString();
            if(!text.equalsIgnoreCase(""));
            int toMin = Integer.valueOf(text);
              min = toMin*60000;


            tv_display.setText("The Phone will lock after: " + text + " minute");
            CountDownTimer countDownTimer = new CountDownTimer(min,10000) {
                @Override
                public void onTick(long ms) {

                }

                @Override
                public void onFinish() {//when the timer end the phone will lock
                    devicePolicyManager.lockNow();
                }
            }.start();

        }
    });
}

}`

3 Answers3

0

Countdown Timer code can be written in a Service. In your MainActivity, use a broadcast receiver to receive broadcasts from the Service.

jay2109
  • 49
  • 1
  • 1
  • 4
0

Instead of declaring the CountDownTimer as a local variable, declare it as an Global variable and make it static

static CountDownTimer countDownTimer;

Hope it works.

Subrata
  • 182
  • 7
0

You design is not correct. A activity component should not exist after the activity is destroyed.

Better option would be to use AlarmManager. Schedule an alarm and you will get a callback to the Receiver you mentioned in the pending intent.

https://developer.android.com/training/scheduling/alarms.html

Android: How to use AlarmManager

or as jay2109 has mentioned, you can also start a Service which will manage the CountDownTimer and use it to broadcast events like in onTick or onFinished in case you want to. (Note: Best practice is to attach and detach broadcast receiver in your activity onPause and onResume. You can use Notification in case you want the user to know that your application did something)

https://developer.android.com/training/run-background-service/create-service.html

send broadcast from Service to Activity?

Seeker
  • 1,030
  • 10
  • 10