I want to build upon my battery saving android app and add the functionality to close all apps excluding user-specified ones.
Im not asking how to kill a process, I want to close the recent apps in the same way that you would by hitting your recent apps button and swiping them all away.
Does anyone know of a way to do this?
It is not recommanded but it is posible by using killBackgroundProcesses of Activitymanager.
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
//get a list of all installed apps.
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;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
Hope it will helps you.
Related
I'm trying to list all apps in android device with queryIntentActivities method but the list doesn't return all the apps , It returns only three of them. Here is my code:
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allApps = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo ri : allApps) {
Log.d("Labels", String.valueOf(ri.loadLabel(packageManager)));
}
is there anyone now why it returns only 3 applications ?
You are most likely trying to do this on Android 11. Make sure you add the <uses-permission android:name"android.permission.QUERY_ALL_PACKAGES"> permission to the AndroidManifest.xml file.
While I haven't tested this aspect of R DP2 yet, it appears that your
app now can't find out what other apps are installed, on a general
basis. The cited example is queryIntentActivities(), but to make this
really work you would need to seriously lobotomize PackageManager. You
can whitelist certain packages and certain structures
to try to get by this for certain use cases. And, this is where the
mysterious QUERY_ALL_PACKAGES permission seen in DP1 comes into play —
this permission removes these new restrictions. Given the "look for
Google Play to provide guidelines for apps that need this permission"
caveat, it is safest to assume that if you try using it, eventually
you will be banned from the Play Store by a bot.
I've integrated some library from the third party into my android app.
They use background service with the location, but it caused java.lang.IllegalStateException error since Android 8 has updated the background location limits.
I've tried to add the following lines to avoid the error but it fails:
if(Build.VERSION.SDK_INT <= 25) { // below Android 8.0
PushAd.startPush(activity);
}else{
PushAd.disablePush(activity);
}
dependencies {
implementation 'com.adlocus:library:3.5.7#aar'
}
Can I delete or remove the library, or stop the service in the main activity programmatically?
[Solved]
I discovered a possible solution to solve the problem, we can use PackageManager to enable/disable a Service/Receiver/Activity from third party library.
ComponentName component = new ComponentName(context, MyReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
Thanks Varun's answer.
Can I delete or remove the library... programmatically?
No.
Can I... stop the service in the main activity programmatically?
Possibly. Ask the developers of the library.
I've tried to add the following lines to avoid the error but it fails
Nobody knows what "it fails" means. Contact the developers of the library and ask them how to use it with Android 8.0+ devices.
I discovered a possible solution to solve this problem, we can use PackageManager to enable/disable a Service/Receiver/Activity from third party library.
ComponentName component = new ComponentName(context, MyReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
Thanks Varun's answer.
Whenever user open any app i need know if application is running on the UI thread. I also need to show a toast of the Package Name of that application.
I have user Activity Manager but it not working as it should. It always shows displayed Launcher.
ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
pack = componentInfo.getPackageName();
Can anyone tell me how I can use usage stats to achieve this?
As far as displaying the name of your package you can use PackageManager and getLaunchIntentForPackage()
We are creating an app where you can share an app, which sends the user to the google play store to download another app.
My question is, how can I know if some user actually downloaded and installed that app?
Is there a some sort of callback you get from the play store?
Try calling startActivityForResult() with the Play Store intent. Then, override the onActivityResult() method in your Activity and check if the app is installed. This might help: How to check programmatically if an application is installed or not in Android?
isAppInstalledBefore("com.example.anappblabla");
private boolean isAppInstalledBefore(String packageName) {
PackageManager pm = getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed; //returns boolean
}
No, but:
What I would do is trying to get the package name (generally in the URL for example: https://play.google.com/store/apps/details?id=dev.laurentmeyer.contactautocompleteview of the target app (which is unique): something like dev.laurentmeyer.whateverApp (it's the one I'm personally using). Then scan the installed apps with this Helper and you'll be sure if it has been installed or not.
I want to extract all the activities names from an android apk file.
Any idea how possibly it can be done?
If you just want to list them from the command line you can use
aapt dump xmltree <apk-file> AndroidManifest.xml
The format is a bit perplexing at first, but all the info is there.
You need the Android SDK installed to do this, of course.
You can use the PackageManager method getPackageArchiveInfo to retrieve the information from an APK file. Assuming your APK lives in the root of your SD card, you can use this code:
String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/MyApp.apk";
PackageManager pm = getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(apkPath,
PackageManager.GET_ACTIVITIES);
//Log.i("ActivityInfo", "Package name is " + info.packageName);
for (android.content.pm.ActivityInfo a : info.activities) {
Log.i("ActivityInfo", a.name);
}
There is plenty of additional information you can find in the ActivityInfo and PackageInfo objects.
You don't have to use the full path to the APK, just call the following directly from inside an Activity.
PackageManager manager = getPackageManager();
PackageInfo packageInfo = manager.getPackageInfo("your package name", PackageManager.GET_UNINSTALLED_PACKAGES);