How to stop opening default apps in android? - android

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?

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.

Related

Which flag for standard apps in PackageManager.getInstalledApplications(flag)?

When I open the Android main menu on my Android smartphone, I get a set of apps like Youtube, Calculator, Email clients etc. No system stuff or any libraries are visible there.
To retrieve these apps programamtically, I do:
PackageManager.getInstalledApplications(flag: Int)
where I get a list of ApplicationInfo, which also contains alot more than mentioned installed standard apps. What flag do I have to set to get only the same apps, which I see when I swipe up on my Smartphone?
val mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)
val appList = context.getPackageManager().queryIntentActivities( mainIntent, 0)
You can try this to get all user apps (and the ones your launcher shows)
You might need to add additional permissions in your manifest above Android 10 though
Edit: If you want ApplicationInfo instead of ResolveInfo in your list you can retrieve it like this:
appList[your_index].activityInfo.applicationInfo

How to determine the application the user is currently typing in?

So if you have ever used the Bitmoji keyboard, you know that it shares your selected Bitmoji in whatever application you are using, be it messenger or SMS without the normal sharing intent pop up, where you choose what application to share it with. This is really my first Android project, as I have been developing for iOS. iOS makes it easy in this case as you just copy the image to the clipboard (pasteboard) and then the user pastes it wherever they want.
Now I'm developing a Android IME, and need to know of a way to know what application the user is currently typing in?
So can someone point me in the direction? I've learned a lot about Android development over the last week, and my head is kind of swimming from reading so much of the documentation, especially with ContentProvider and having images share to the Android SMS application correctly.
Try the below code to get the package name:
ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
}
else{
String mPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
I wanted to post the answer to this question in case someone is in need of the solution. First you need to use the EditorInfo class of the InputMethod to get the packageName of the current InputConnetion using :
EditorInfo editorInfo = getCurrentInputEditorInfo();
String inputConnectionPackageName = editorInfo.packageName;
Then you need to query your intent activities to see what applications the user has that will handle the intent you are trying to send. Using :
PackageManager pManager = getPackageManager();
List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
mApps = pManager.queryIntentActivities(intent,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
Then all you will need to do is create a For loop and compare:
for (int i = 0; i < mApps.size()-1; i++) {
ResolveInfo info = mApps.get(i);
if (info.activityInfo.packageName.equalsIgnoreCase(editorInfo.packageName)) {
intent.setPackage(info.activityInfo.packageName);
//send out the intent
startActivity(intent);
}
}

Kill another Background Application in android

Problem: I want to kill a background application process.
We call the below methods inside a background thread/services and it's not working.
We have tried a few methods available on net but not succeeding to kill the background process/application.
My Device has a root permisision already.
Code here
1st Method:
int value = findPIDbyPackageName("com.google.android.youtube");
android.os.Process.sendSignal(value, 9);
2nd Method:
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.google.android.youtube");
3rd Method:
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
activityManager.restartPackage("com.google.android.youtube");
4th Method :
android.os.Process.killProcess(pid);
5th Method : `
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue; // here my package defines your application package entered in manifest
else if(packageInfo.packageName.equals("third Party application Package Name")) // if you dont have this package name then prefer playstore url of this app to get packagename
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
6th
We launched a third party app like youtube, Subway surf from our own android app.
We are using startActivityForResultmethod for launching the app.
launchApp("com.imangi.templerun");
protected void launchApp(String packageName) {
mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (mIntent != null) {
try {
startActivityForResult(mIntent, 101);
} catch (ActivityNotFoundException err) {
Toast t = Toast.makeText(this, "App not found", Toast.LENGTH_SHORT); t.show();
}
}
}
To close the application, the method finsihActivity(ResposneCode) is available.
But we're not able to use it in service.
You are not allowed to kill processes which don't belong to your app. The system will decide when to kill and what to kill when necessary.
App intervention is just to tell the system a message, Please help me to kill this process, blah blah..., that's all.
Process.killProcess() should work, this is your method number 4.
Read carefully what it says.
Kill the process with the given PID. Note that, though this API allows
us to request to kill any process based on its PID, the kernel will
still impose standard restrictions on which PIDs you are actually able
to kill. Typically this means only the process running the caller's
packages/application and any additional processes created by that app;
packages sharing a common UID will also be able to kill each other's
processes.
Under root permission you should be able to remove the kernel restrictions, unfortunately I do not know how to do this, I suggest you to search for native solution for that, may be some C codes, there are plenty of them in the net.
Also in your method 1 you been trying to send SIGNAL_KILL, try sending SIGNAL_QUIT

How to filter some of the application apps?

I need to display a screen with all the installed applications. I can do this already, but I need to filter some of the system applications. I am doing like this:
if((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
This works, but the problem is that it hides apps like System, Video Player, and Sound Recorder. However, I need these apps to show up, too. The question is, how to do this?
You'll have to filter them manually via their process-name, e.g.:
if(appInfo.packageName().equals("com.android.soundrecorder"))
Please post more of your code if this doesn't work!
If an Application is a non-system application it must have a launch Intent by which it can be launched. If the launch intent is null then its a system App.
Example of System Apps: "com.android.browser.provider", "com.google.android.voicesearch".
For the above apps you will get NULL when you query for launch Intent.
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo:packages){
if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
String currAppName = pm.getApplicationLabel(packageInfo).toString();
//This app is a non-system app
}
else{
//System App
}
}

Get list of installed android applications

Hi I want to get a list of all of the installed applications on the users device I have been googling for the longest time but can't find what i want this link was the closest though and works fine except me being new don't understand how to use the method getPackages(); and create a list with it
http://www.androidsnippets.com/get-installed-applications-with-name-package-name-version-and-icon
Any help on how to create the actual list would be a major help i have all that code already in just can't get the list to actually show thanks for any help
I was working on something like this recently. One thing I'll say up front is to be sure and perform this in a separate thread -- querying the application information is SLOW. The following will get you a list of ALL the installed applications. This will include a lot of system apps that you probably aren't interested in.
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
To limit it to just the user-installed or updated system apps (e.g. Maps, GMail, etc), I used the following logic:
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
}
}
EDIT: Also, to get the name and icon for the app (which is probably what takes the longest -- I haven't done any real deep inspection on it -- use this:
String label = (String)pm.getApplicationLabel(app);
Drawable icon = pm.getApplicationIcon(app);
installedApps should have a full list of the apps you need, now. Hope this helps, but you may have to modify the logic a bit depending on what apps you need to have returned. Again, it is SLOW, but it's just something you have to work around. You might want to build a data cache in a database if it's something you'll be accessing frequently.

Categories

Resources