javaandroidcountdownandroid-package-managersdevice-policy-manager

How to programmatically set a lock or pin for an app


So right now I am trying to develop an Android App for my young children. I want to set a pin or passwords on selected applications for a particular amount of time to prevent them from opening the app. For example, let's say that my daughter wants to play angry birds for some time on my phone while I am doing work. I will select my important apps like messaging, gmail, etc and put a pin or password on it for 30 minutes while she plays angry birds. After 30 minutes, I get my phone from my daughter and I can open the app without a pin because the time limit expired.

I have done a ton of research on this, but I haven't been able to find an implementation for my particular case.

How do android "app lock" applications work?

I know that app lock has a similar kind of structure of what I want to do. I just to place a time limit for the lock.

https://play.google.com/store/apps/details?id=com.domobile.applock&hl=en

I am staying away from killing activities/ applications with ActivityManager etc. I really just want a clean lock screen over a selected app for a particular amount of time.

I have a CountdownTimer to count down the timer for a time I set. How would I modify this code to block certain applications for a chosen amount of time if I had all the packagename?

    start_timer.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {

            new AlertDialog.Builder( MainActivity.this )
                    .setMessage( "Are you sure you want to block the selected apps for the set amount of time?" )
                    .setPositiveButton( "Yeah man!", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Log.d( "AlertDialog", "Positive" );

                            hourint = Integer.valueOf(number_text.getText().toString());

                            minuteint = Integer.valueOf(minute_text.getText().toString());

                            secondint = Integer.valueOf(second_text.getText().toString());

                            Log.i("YourActivity", "Hours: " + hourint);

                            Log.i("YourActivity", "Minutes: " + minuteint);

                            Log.i("YourActivity", "Seconds: " + secondint);

                            totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000;      // time count
                            timeBlinkInMilliseconds = 30*1000;

                            countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
                                // 500 means, onTick function will be called at every 500 milliseconds

                                @Override
                                public void onTick(long leftTimeInMilliseconds) {
                                    Context context = MainActivity.this;





                                    long seconds = leftTimeInMilliseconds / 1000;
                                    mSeekArc.setVisibility(View.INVISIBLE);
                                    start_timer.setVisibility(View.INVISIBLE);
                                    block_button1.setVisibility(View.INVISIBLE);



                                    if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
                                        // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
                                        // change the style of the textview .. giving a red alert style

                                        if ( blink ) {
                                            number_text.setVisibility(View.VISIBLE);
                                            minute_text.setVisibility(View.VISIBLE);
                                            second_text.setVisibility(View.VISIBLE);


                                            // if blink is true, textview will be visible
                                        } else {
                                            number_text.setVisibility(View.INVISIBLE);
                                            minute_text.setVisibility(View.INVISIBLE);
                                            second_text.setVisibility(View.INVISIBLE);


                                        }

                                        blink = !blink;         // toggle the value of blink
                                    }

                                    second_text.setText(String.format("%02d", seconds % 60));
                                    minute_text.setText(String.format("%02d", (seconds / 60) % 60));
                                    number_text.setText(String.format("%02d", seconds / 3600));                     // format the textview to show the easily readable format
                                }


                                @Override
                                public void onFinish() {
                                    // this function will be called when the timecount is finished
                                    //textViewShowTime.setText("Time up!");
                                    number_text.setVisibility(View.VISIBLE);
                                    minute_text.setVisibility(View.VISIBLE);
                                    second_text.setVisibility(View.VISIBLE);
                                    mSeekArc.setVisibility(View.VISIBLE);
                                    start_timer.setVisibility(View.VISIBLE);
                                    block_button1.setVisibility(View.VISIBLE);


                                }

                            }.start();
                        }
                    })
                    .setNegativeButton("Nope!", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Log.d("AlertDialog", "Negative");
                            dialog.cancel();
                        }
                    })
                    .show();

EDITED: http://pastebin.com/MHGFw7PK


Solution

  • Logic

    Now Code Example:


    Edit: (Get Top Package Name for Lollipop)

    A very good answer is here.