I am trying to make an android app for users in which user can activate Child protection. for example, User opens my app, he can view all the apps he has (including default ones). Now if the user blocks default message or calls app then everything should be same, just these selected app must not open, even after anyone tap on these applications multiple time. It should look like nothing happening. I don't want to have a privacy PIN code or pattern on that app. I just want to stop the children by opening any app of mobile selected by the user through my application. Is this possible? if yes then any idea about how can i achieve this thing?
-
1First of all **Don't post your idea** :D:D, Secondly this is not correct forum for this question. – Vikasdeep Singh Jul 12 '18 at 07:37
-
Actually may be you are right about Idea. but if any 1 else making this thing then i dont have any issue because what i am making, others cant. second thing i just want to have a code for blocking app like this. – Uzair Qaiser Jul 12 '18 at 07:41
1 Answers
This problem is very similar to the one answered here, that will be my source for this answer. I can't try this code, but I hope it can give you a rough idea to make your first try and see yourself if it work or not.
The idea
The basic idea is to create a Background Service
that continuously checks if there are open apps. If those apps are on the list the user locked, the show a lock screen. I don't know if it is possible to close an opened app¹ (I hope it's not!), but for your parental control functionality, a lock screen should have the same effect.
Select the apps to lock.
To retrieve from the system all apps that are installed
PackageManager packageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
/* Uncomment this to exclude system apps
if ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
continue;
} */
appList.add(p.packageName);
}
Now appList
is a List<ResolveInfo>
, and you can show to the user a ListView
to let her choose the apps to be locked. Save these somewhere (SharedPreferences
is an idea).
The service
What the service has to do is to:
Check the currently opened app:
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop = ar.topActivity.getClassName();
Check if activityOnTop
is in the locked-apps list.
for(String appName : appList) {
if(appName.equals(activityOnTop)) {
// This app is locked!
LockApp(); // Defined later
break;
}
}
If so, start an Intent
to your LockScreen
activity (that won't allow any action)
private void LockApp() {
Intent lockIntent = new Intent(mContext, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(lockIntent);
}
I give a lot of things for granted: you have to start a Service somewhere, you have to build a couple of activities, and this process does not avoid the problem of a smart kid that closes the process in background ... But I think this can work as a starting point.
¹ It apparently is possible. Give a look to this answer, an idea is to substitute the code in LockApp()
with something like that.
This doesn't change that I think a Lock Screen is more elegant.

- 706
- 8
- 25
-
I really appreciate your help about creating list of apps and other things. its really good for start. but I dont want to start any activity. I already blocked my outgoing calls using broadcast receiver. I dnt want to start any Intent so dat user knows that something hapening right now. – Uzair Qaiser Jul 12 '18 at 08:21
-
According to this: https://stackoverflow.com/questions/40797866/how-to-close-foreground-app-in-android-service/40797896#40797896 , _You can only kill a process that has the same package id as the one that is doing the killing [...], unless you have a rooted device and your application has root privileges._ So, if you do not want to force the user to root the device, I think the task will be very hard. – Simone Chelo Jul 12 '18 at 08:29
-
You are right @Simon, I do need the root privileges if i want to do this. but if i can check about running apps, then is it possible to check continuously on opening of every app that if its in lock list then just dont open it. Do nothing like starting another activity or etc. just dont open it.untill its empty from my list app – Uzair Qaiser Jul 12 '18 at 08:56
-
and i am not killing process of an open application. I trying to stop user by opening any app process – Uzair Qaiser Jul 12 '18 at 09:34