1

I have to implement application wide lock. This lock works in two situations.

  1. When the phone is awake and app is running on foreground, Dialog box appears after time lapsed to unlock.(time lapsed is saved in shared preference)
  2. When the phone is asleep or user pressed the Home button/Side power Button with app running on screen, same dialog box appears on resume.

I know how to implement them separately by using timer(1st req.,foreground) and alarm manager(2nd req,background). IS there any possibility if both can be done with only one type of lock...I want to do that using alarm manager.

I have refered this:Lock android app after a certain amount of idle time

Application file

public class MyApplication extends Application implements PinPickerDialogHandler, OnClickListener
{
    public void updateIdle()
    {
     final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
     idle = preferences.getBoolean("idle_switch", false);
     idle_delay = preferences.getLong("idle_time", 60000);
    }
    public void resetTimer() {
     mTimer.cancel();
     mTimer.purge();
     mTimer = new Timer();
     mTimer.schedule(new TimerTask()
        {
            @Override
            public void run() {
                if(idle){
                    mCurrentActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showLockDialog();
                        }
                    });
                }
            }
        }, idle_delay);
    }
public void resume(Activity act) {
            Timeout.cancel(act);        
        // Check whether the timeout has expired
        long cur_time = System.currentTimeMillis();
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(act);
        long timeout_start = preferences.getLong("Timeout_key", -1);
        // The timeout never started
        if (timeout_start == -1) {
            return;
        }   
        //long sTimeout = preferences.getLong("idle_delay", 30000);//App_timeout_key is the idle time
        long timeout;
        try {
            //timeout = Long.parseLong(sTimeout);
            timeout=idle_delay;
        } catch (NumberFormatException e) {
            timeout = DEFAULT_TIMEOUT;
        }
        // We are set to never timeout
        if (timeout == -1) {
            return;
        }
        if (idle){
        long diff = cur_time - timeout_start;
        if (diff >= timeout) {
            // We have timed out,call method for dialog box using reflections   
            //Toast.makeText(act, "We have timed out", Toast.LENGTH_LONG).show(); 
            showLockDialog();
        }
        }
    }
}

Main Activity

public class BaseActivity extends Activity {
    protected MyApplication mApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApp = (MyApplication)this.getApplicationContext();
        mApp.setCurrentActivity(this);
        mApp.resetTimer();
        mApp.pause(this);
        }

    @Override
    protected void onResume() {
        super.onResume();
        mApp.setCurrentActivity(this);
        mApp.resetTimer();
        mApp.resume(this);

    }

    @Override
    protected void onPause() {
        mApp.cancelTimer();
        super.onPause();
        mApp.pause(this);
    }



    @Override
    protected void onDestroy() {    
        clearReferences();
        Timeout.cancel(this);
        mApp.cancelTimer();
        super.onDestroy();


    }

    @Override
    public void onUserInteraction () {
        mApp.resetTimer();
        mApp.pause(this);
    }

The Timer.java and Timerservice.java are almost the same as post I have referenced.Even if I remove these file the code shows the same behavior.PLease suggest a way to implement this properly.

Community
  • 1
  • 1
Chetandalal
  • 674
  • 1
  • 7
  • 18

0 Answers0