-1

I am trying to build a parental control app. So now i want to disable or lock app (like Whatsapp, Facebook, etc). I have tried using PackageManager.setComponentEnabledSetting(). But it is throwing java.lang.SercurityException.

So how can I make a parental control app such that I can disable any app I want without root.

my code is

 pm.setComponentEnabledSetting(new ComponentName(temp.activityInfo.packageName,
                        temp.activityInfo.name+".class"),
                        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP);

my error was this

java.lang.SecurityException: Permission Denial: attempt to change component state from pid=11537, uid=10067, package uid=10029

MikaelF
  • 3,518
  • 4
  • 20
  • 33
  • As a suggestion you can Add conditions in your splash screen activity to check if the app is unlocked by parent or not, you can save lock/unlock detail on a server – Rahul Gaur Feb 10 '20 at 03:49
  • 1
    You describe a parenting control, I read ransomware (not saying this is your idea, just that this could be use as it) This would be a reason why this might not be possible to restrict the access of any app. – AxelH Feb 10 '20 at 06:45

2 Answers2

0

You must add below permissions to manifest.

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

but , these permissions are for System apps and you can not use. :(

Ali Bagheri
  • 3,068
  • 27
  • 28
0

You can not write a app to lock or close another app.this is a policy in Google. for lock a app you must check running apps repeatedly, if specific app is open,then show a activity over that.

while(!Thread.currentThread().isInterrupted())
{
    String topActivity = getFrontApp();

    if(topActivity.isEmpty())
    {
        threadSleep(500);
        continue;
    }


    if(topActivity.equals("lockApp"))
    {
        showLockActivity();
    }

    threadSleep(500);
}

// for Api21+ need permission
    public static String getFrontApp()
    {
        if (Build.VERSION.SDK_INT >= 21)
        {
            UsageStatsManager usageManager = SystemMaster.getUsageStatsManager();
            long now = System.currentTimeMillis();
            List<UsageStats> localList = usageManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, now - 900_000L, now);
            String str = "";

            if (localList != null)
            {
                SortedMap<Long,UsageStats> mySortedMap = new TreeMap<>();

                for(UsageStats usageStats : localList)
                    mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);

                if(!mySortedMap.isEmpty())
                    str =  mySortedMap.get(mySortedMap.lastKey()).getPackageName();
            }

            return str;
        }
        else
        {
            ActivityManager am = (ActivityManager) getApplication().getSystemService(Context.ACTIVITY_SERVICE);
            return am.getRunningTasks(1).get(0).topActivity.getPackageName();
    }

above code is very simple , for real app must write more.

Ali Bagheri
  • 3,068
  • 27
  • 28