0

I am basically working with Android Application , i have requirement to restrict to open others apps while my app is running, Means My app is running it will put some locks on other application while i am accessing the others app, Kind of Application Lock. I am trying but could not find good solution. If any buddy have some idea or link or sample code please share to me.

2 Answers2

0

Basically you want Kiosk Mode .

Try this:

  1. Home button press: <category android:name="android.intent.category.HOME" /> //add in manifest

What id does: whenever you press the home button, all the applications installed in your phone which have category.HOME category in intent-filter in their AndroidManifest.xml will be listed .

  1. then handle Long press Home Button:

        @Override
        protected void onPause() {
            super.onPause();
        ActivityManager activityManager = (ActivityManager) getApplicationContext()
                    .getSystemService(Context.ACTIVITY_SERVICE);
    
            activityManager.moveTaskToFront(getTaskId(), 0);
        }
    

for this add this line in manifest :

<uses-permission android:name="android.permission.REORDER_TASKS" />

3.handle BackButton :

@Override
public void onBackPressed() {
    // Stop user to exit
    // super.onBackPressed();
}

You can also try this for Kiosk Mode: Read this document

  • Thanks Harshikesh :) Actually i have one service, what service will do for me, Get all package name or running foreground process, then i have to pass intent to lock Activity, but intent not pass to Lock Activity. – Abhishek Chandale Sep 01 '15 at 08:03
0
    import android.app.ActivityManager;


    import android.app.Service;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
        import android.content.SharedPreferences;
        import android.os.IBinder;
        import android.provider.SyncStateContract;
        import android.util.Log;
        import java.io.File;
        import java.util.ArrayList;
        import java.util.HashSet;
        import java.util.Iterator;
        import java.util.List;
        import java.util.Map;
        import java.util.Set;
        import java.util.Timer;
        import java.util.TimerTask;
public class HeartBeat extends Service {



    private static final String TAG = HeartBeat.class.getSimpleName();
    public Timer TIMER;



//    private static Set<AccessGranted> mAccessGrantedList = new HashSet<AccessGranted>();
    private Set<String> mLockedApps = new HashSet<String>();
    private long lastModified = 0;
    private BroadcastReceiver mScreenStateReceiver;
  //  private BroadcastReceiver mAccessGrantedReceiver;
    private File mLockedAppsFile;
    ArrayList<String> packagezList;
    SharedPreferences sharedPrefs;
    Map<String, ?> allEntries;
    SharedPreferences sharedPrefsapp;

    String prefix;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startService(new Intent(this, HeartBeat.class));

         Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        if (TIMER == null) {

            TIMER = new Timer(true);
            TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);

            mScreenStateReceiver = new BroadcastReceiver() {

                private boolean screenOff;

                @Override
                public void onReceive(Context context, Intent intent) {



                    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                        screenOff = true;
                    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                        screenOff = false;
                    }

                    if (screenOff) {
                        //Log.i(TAG, "Cancel Timer");
                        TIMER.cancel();
                    } else {
                        // Log.i(TAG, "Restart Timer");
                        TIMER = new Timer(true);
                        TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);
                    }
                }
            };

            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            registerReceiver(mScreenStateReceiver, filter);


        }
        // this.stopSelf();
        //startforeground goes here
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        startService(new Intent(this, HeartBeat.class));
    }

    private class LockAppsTimerTask extends TimerTask {

        @Override
        public void run() {


            sharedPrefs = getApplicationContext().getSharedPreferences(getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
            sharedPrefsapp = getApplicationContext().getSharedPreferences("appdb", Context.MODE_PRIVATE);
            allEntries= null;
            allEntries = sharedPrefsapp.getAll();

            Set keySet = allEntries.keySet();

            Iterator<String> keySetIter = keySet .iterator();
            while (keySetIter.hasNext()) {
                String keyEntry= keySetIter.next();
                Log.e("Shared Vaues",keyEntry);
            }

            //prefix = "m";
            packagezList= null;


            packagezList = new ArrayList<String>();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                packagezList.add(entry.getKey());
                Log.e("right key: ", entry.getKey().toString() + "right value: " + entry.getValue().toString());
            }


/*        for (Map.Entry<String, ?> entry : allEntries.entrySet())
        {
            //Check if the package name starts with the prefix.
            if (entry.getKey().startsWith(prefix)) {
                //Add JUST the package name (trim off the prefix).
                packagezList.add(entry.getKey().substring(prefix.length()));
            packagezList.add(entry.getKey());

            }
        }*/

            for(Object object: packagezList){
                Log.e("YO!", (String) object);
            }


            ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);

            try {
                //List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
                ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
                        .getRunningTasks(1);
                ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
                String activityOnTop = ar.topActivity.getPackageName();

                Log.e("activity on Top", "" + activityOnTop);
                Log.e(" My package name", "" + getApplicationContext().getPackageName());
                //for (Object data : newArrayList) {

                for(Object object: packagezList){

               // Provide the packagename(s) of apps here, you want to show password activity
                if (!activityOnTop.contains(getApplicationContext().getPackageName()))

                  //  if(!activityOnTop.contains(getApplicationContext().getPackageName()))
                    {  // you have to make this check even better
                        Intent i = new Intent(getApplicationContext(), LockScreenActivity.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        i.putExtra( "", "");
                        startActivity(i);
                    }

                }


            } catch (Exception e) {
                Log.e("Foreground App", e.getMessage(), e);
            }
        }



    }