What is the alternate for clearApplicationUserData() from PackagerManager class in android? - java

I have a requirement for clearing application user data and cache data for provided package name. Previously I was using clearApplicationUserData(), deleteApplicationCacheFiles() methods from PackageManager class. But now both are deprecated or hidden (#hide) in android 11. What's the alternative?
Below is my previous way of doing it -
PackageManager pm = mContext.getPackageManager();
//observer is ->IPackageDataObserver
pm.clearApplicationUserData(packageName, observer);
and
PackageManager pm = mContext.getPackageManager();
//observer is ->IPackageDataObserver
pm.deleteApplicationCacheFiles(packageName, observer);
What would be the alternate solution for the same? I know there are methods like -
(mContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).clearApplicationUserData()
//for clearing current application user data
and
mContext.getCacheDir().deleteRecursively()
//for clearing current application cache data
But in my case, it is another application and I will provide the package name.
I was reading about clearApplicationUserData() from DevicePolicyManager class, but not sure how it works. Can anyone help me here?

Related

List the all apps in Android

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.

Q: Android Dev - Enable a Manifest-Disabled Broadcast-Receiver from Activity

I have a BroadcastReceiver which is registered in my Manifest file as "Disabled"..
It listens for a System-based Broadcast, and then fires.. but I don't want my BroadcastReceiver to actually be Enabled and Listening until I tell it to (for example, a user-set Preference within my app which makes it Enabled)..
From what i've gathered by searching, I've found the following 2 things that might work:
Context.getApplicationContext().registerReceiver
(LocationReceiver.class,
"android.intent.action.PROVIDER_CHANGED");
and
setComponentEnabledSetting
(LocationReceiver.class,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
0);
But neither of them are working..
For the first one, it says "the non-static method getApplicationContext cannot be referenced from a static context, and the second one I think I may just not be referencing the first argument correctly (ComponentName).
Can anybody give me some insight?
Thanks!
Try this hope it will work
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP)

Any way to close recent apps in code?

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.

Android app expansion packs

How would I implement expansion packs on the market? Like if I wanted users to be able to purchase more images, how would I do that and also have the main app recognize and load those images? Are there any tutorials you can point me to? I searched using Google but didn't find anything very specific and beginner friendly. Most were in regards to in app billing but that's not what I'm looking for.
You can use PackageManager to look for your expansion packs. The exact way in which you would identify your expansion packs depends on how you're going to use them.
For example, if your expansion packs are runnable in some way - i.e., they all have a main activity - then you'd probably have a custom intent action, and you'd use PackageManager.queryIntentActivities() to give you a list of installed activities that can handle that action.
If these expansion packs are providing just resources and not code, you can choose to either have a "dummy" component that you can identify by intent as detailed above - even if you have no intent of ever running it - or you can look for another approach. For example, if no one but you will be making these expansion packs, you may simply identify them by package name.
PackageManager pm = getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages();
for(PackageInfo package : packages) {
if(package.packageName.startWith("com.myapp.extension.") {
// found an extension package!
handleExtensionPackage(package);
}
}
If you need to load a resource from an extension package, you'll need to create a context for that package, look up the resource identifier, and then load the resource.
Context extensionContext = createPackageContext(package.packageName, 0);
Resources extensionResources = extensionContext.getResources();
int r_drawable_pic = extensionResources.getIdentifier("drawable/pic", null, package.packageName);
Drawable remotePic = extensionResources.getDrawable(r_drawable_pic);

Android Shared Preferences Updating

I have two projects/packages part of the same application. The main project of the application does virtually everything on the app. However, we used a 2nd project to manage updates to the application. We are using shared preferences that are updated from the sqlite3 database and applied in the main application using
editor.putString("string", sString).apply();
We also use the following logic to see if its a new version and if so restart the autoupdate package
if (!sCurrentVersion.equals(ver)) {
Intent intent1 = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
intent1 = manager.getLaunchIntentForPackage("com.pack.autoupdate");
intent1.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent1);
}
Then in the autoupdate package, we simply just use getSharedPreferences(). The values we need in the shared preferences are the update urls that the autoupdate package needs to check.
Our problem is that we updated those urls in the database, however we are still getting some devices that are using the old urls in the autoupdate check. Do I need to make sure and restart autoupdate or is there something i'm doing wrong with sharedpreferences?
Did you do call the .commit() method to update your SharedPreference value?
Update :
More information regarding the difference between .commit() and .apply() Here

Categories