Is there any way to Clear all Cache files programmatically? - android

I'm developing a program that delete the cache of all programs. This has been asked many times before but the solutions were either removed in newer versions of Android or didn't clear all caches or they needed to root the device.
I found a solution in Android docs that added in api 30 and should be launched using Activity#startActivityForResult(Intent, int) so that the user knows which app is requesting to clear cache.
this action requires Manifest.permission.MANAGE_EXTERNAL_STORAGE in manifests.
I have this permission in my manifest and I using following code in my OnClick method:
Intent clearCache = new Intent();
clearCache.setAction(ACTION_CLEAR_APP_CACHE);
startActivity(clearCache);
But it doesn't work in my Android 11 device. Does anyone know code problem?
Thanks.

This intent should be launched using
Activity#startActivityForResult(Intent, int) so that the user knows
which app is requesting to clear cache.
Check the documentation.
Use "startActivityForResult" instead of "startActivity":
//Get cache dialog
int requestCode = 999;
Intent intent = new Intent(StorageManager.ACTION_CLEAR_APP_CACHE);
activity.startActivityForResult(intent, requestCode);

Related

Android 10 - file provider - permission denial

I'm facing a weird issue trying to provide a file in Android 10. My code works fine in Android 9 and 11 but in Android 10 only works 50% of the times. There are no difference on the system status when it works and when it doesn't.
The intent:
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(file, UpdateHelper.APK_TYPE)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
If I grant the permission explicitly, it always work. So my question is not how to fix it, but if there are any explanation for it that I'm missing. In my understanding granting the permission in the intent is the preferred way of doing it.
context?.grantUriPermission(packageName, file, Intent.FLAG_GRANT_READ_URI_PERMISSION)
Reviewing the documentation, it seems to be a race condition.
Permissions granted in an Intent remain in effect while the stack of the receiving Activity is active. When the stack finishes, the permissions are automatically removed.
For some reason this does not affect Android 9 and 11. But in Android 10 does.
The reason for my app to be finished is that I'm using this to pass a new apk to the packageInstaller in order to update the app.

Delete app intent not working on android pie

This worked in the past but does not work on my android pie device anymore (worked until the recent pie update):
fun uninstallApp(packageName: String) {
val packageURI = Uri.parse("package:$packageName")
val intent = Intent(Intent.ACTION_DELETE, packageURI)
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent )
}
I could not find any documentation that indicates that this intent is not working anymore.
Does anyone know if there is an alternative way to open the uninstall dialog on android pie?
The code will still work, supposedly the app is not requesting the permission to execute it.
Since Android Pie (Android 9), apps are required to declare that they request apps to be deleted. This can be done by adding this permission to the AndroidManifest.xml:
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
* Note that it is not needed to request this permission at runtime. Declaring this in the Manifest is enough for it to work.
Also, we could use the ACTION_UNINSTALL_PACKAGE action instead for the request of removing packages. For this action, the documentation is mentioning that the permission stated above is required for it to work since Android Pie.

Usage access settings, permissions

I am trying to use UsageStatsManager. I know that I am supposed to include.
<user-permission android:label="PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions"/>.
I also started an intent for the user to allow usage access.
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
But my app does not show up in the Apps with usage access list in the settings.
Screenshot
I need to know why my app is not showing up in that list?
Your tag is misspelled. its supposed to be <uses-permission/> instead of <user-permission/> RIP

How to remove all images which came with the app when uninstalling

i am creating a simple app for a project that comes with an image and above it a button when you press the button the image becomes the wallpaper of the phone.
This i have done however for my project i need the wallpaper to be removed if the app is uninstalled how do i do this?
Save your images here this folder will be deleted when you uninstall the app
If you look at the Android Documentation for the Intent ACTION_PACKAGE_REMOVED, you will see that this broadcast action will be received by any other interested application (other apps who are registered to receive this broadcast), other than your own application.
Quote from the developer.android.com site:
Broadcast Action: An existing application package has been removed
from the device. The data contains the name of the package. The
package that is being installed does not receive this Intent.
Unfortunetaly, because your app can never detect that it is being removed, you cannot trigger a specific function that will change the Wallpaper on the device. If it were possible (which it isn't), a simple function (code follows) would have shown the Select Wallpaper chooser to the user.
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(intent, "Select Wallpaper"));
It is something the user will have to manually change once he / she has uninstalled your application.
That being said, as far as deleting all your app created files are concerned, let Android handle that for you. Use one of these options depending on the API:
API greater 8 or greater: getExternalCacheDir
API 7 or lesser: getExternalStorageDirectory.
Something like this will let you determine which one to use depending on the device API:
int currentAPIVersion = android.os.Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.ECLAIR) {
// USE getExternalStorageDirectory
} else {
// USE getExternalCacheDir
}

Kill an app / package in API 8 (Froyo)

I have an app killing feature in one of my apps but up to API 7 i always worked with restartPackage(String PackageName); but since API 8 its deprecated so I tried killBackgroundProcesses(String PackageName); but that didn't work either.
Both are methods are in the ActivityManager class
I hope someone can help me.
I have, or rather had, the same feature in one of my apps... From all the research that I have done this feature is no longer possible.
The SDK Docs state this about why the restartPackages permission was deprecated:
"This is now just a wrapper for killBackgroundProcesses(String); the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc."
This seems to imply that anything visible to the user can no longer be closed by SDK applications. I am very disappointed by this decision and if anyone knows of a workaround I am interested in the answer as well.
I understand that there is the potential to "break" other applications with this feature enabled, but I thought that this is what the whole permission system is for. Users know up front the permissions that the app grants and thereby know what the possible consequences are.
I don't know how many people have come to me asking me to fix the fact that they can no longer close applications in FroYo via my application.
EDIT:
The best thing I have been able to come up with is to add the ability to provide a one-click solution go to the System's Application Info page for a given application. Below is some example code that I use in my app:
public Intent getManagePkgIntent(String pkgName)
{
int osVersion = AppMode.getAndroidVersion();
Intent intent = new Intent();
if (osVersion > AppMode.FROYO_SDK_VERSION)
{
//Settings.ACTION_APPLICATION_DETAILS_SETTINGS - THIS CONSTANT ISN'T AVAILABLE UNTIL COMPILING WITH 2.3
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", pkgName, null));
return intent;
}
else //FROYO And Older...
{
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
String pkgExtra = (osVersion != AppMode.FROYO_SDK_VERSION) ? "com.android.settings.ApplicationPkgName" : "pkg";
intent.putExtra(pkgExtra, pkgName);
}
if (m_pkgMgr.resolveActivity(intent, 0) == null)
return null;
return intent;
}
AppMode.getAndroidVersion() is just a static method that safely gets the Android OS version as an int (because the app also runs on 1.5)...
AppMode.FROYO_SDK_VERSION is just a static final int indicating the FroYo API level.
There is one article talked about this issue.
In android 2.2, there is still "Force Close" button in the emulator test.
So this means it still has the way to overcome the disappeared "restartpackage" function.
But I am not sure it is Public API or just only allows the System level to use. Hope the answer is the former one. :(
link text

Categories

Resources