I have created an application which is working as a launcher. In that application I have opening various other apps. In that app I want to clear all the data(i.e login details, search history) of other application to be removed on click of a button.
PackageManager pm = getPackageManager();
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m:methods){
if(m.getName().equals("freeStorage")){
try{
long desiredFreeStorage = Long.MAX_VALUE;
m.invoke(pm,desiredFreeStorage,null);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
Using the above code, I am able to delete the cache but not able to delete the data of other apps.
Please provide me a solution for that.
Sorry for my bad english.
Its normal behavior. you can't delete data that belongs to other Apps via your app. Files that contain data are private and only accessible by Apps that own them or shared by App.
For example, most of the Apps use SharedPreference as private files to save data for later use. So, its senseless if other Apps are allowed to delete that data.
Related
I have a doubt, how I can delete a folder on Android 11 (or 10)?
Have much answers here on Stack how to do it, but nothing of worked.
This code worked for me on Android 5:
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
On newest versions of Android it not work. I noticed that there are applications that can to do this, so I imagine it is possible. Any answer about it?
Android has done much to change its permission models around file access since Android 10 and 11. The preferred approach is to use Scoped Storage APIs.
Many of the old file read/write permissions are on life support and will no longer work. If they do continue to work, then must justify to Google why you need them, and then go through a security approval by Google. You will only be granted approval if you app is classified as a file browser application, or the like.
Basically, the new model is to use Android's file browsers to gain access to read/write/update a particular file that the user selects, and defer the rest of the file management to Google's first-party applications. The access you get is based on what the user selected in the first-party file browser. You are then handed a URI back to your application with the proper permissions to perform the intended action, such as read/write/etc...
You may find this video useful:
https://www.youtube.com/watch?v=RjyYCUW-9tY
Have you tried Context.deleteFile() ?
getApplicationContext().deleteFile(filename);
since Enviroment.getExternalStorageDirectory() is deprecated I have a little Problem. I have 2 applications (a lite and a pro version of an app). These apps have a database which I could export and import. When switching from lite to pro app, the user could import the "old" database into the new app. The database was stored under a folder inside his "sdcard". But now on Android 12 devices we no longer have access to that External Storage.
So how could I solve the problem?
Cause I think, context.getExternalFilesDir((Environment.DIRECTORY_DOCUMENTS)
doesn't work cause the namespace of both apps are different. And I guess it will not work to open a file which is not located under the namespace of the app the file is requested.
Yes, sharing the app's shared memory with another one is not possible.
Other options are using a external server / Firbase to create profile for users and store there data in it.
The easiest way is not to have two apps. Make the single app work for both modes, and gate features at runtime based on the type of license. You can even make the upgrade an in app purchase.
I found a solution by myself, but if someone else is looking for a solution it might be helpful.
I ask the user to specify a file location via
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/db");
intent.putExtra(Intent.EXTRA_TITLE, "xyz.db");
mCreateNewDatabaseFile.launch(intent);
then in the mCreateNewDatabaseFile
private final ActivityResultLauncher<Intent> mCreateNewDatabaseFile = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK) {
if (result.getData() != null && result.getData().getData() != null) {
Uri path = result.getData().getData();
getContentResolver().takePersistableUriPermission(path, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(path, Intent.FLAG_GRANT_READ_URI_PERMISSION);
mPreferences.edit().putString(PREF_KEY_DATABASE_EXPORT_PATH, path.toString()).apply();
}
}
});
So even with another process I can read and write the file.
So in the lite Version I can create the db file and in the pro version I can let the user choose this file for import.
I hope it will help others with similar questions.
You can (should) use a ContentProvider to have other apps query your database using accessible methods of your ContentProvider :
you can configure a content provider to allow other applications to
securely access and modify your app data
(this will enforce Single-responsibility principle)
I have an application at the Google Play and there are already a few hundred of users.
Now, I'm working on an update that will allow users to share user-to-user content via Firebase Dynamic Links.
In order to do so, I created a subdomain and all the required things and I added this code:
FirebaseDynamicLinks.getInstance()
.getDynamicLink( getIntent() )
.addOnSuccessListener( this, pendingDynamicLinkData -> {
Uri deepLink = null;
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
String bookID = deepLink.getQueryParameter( "id" );
if (bookID != null) {
startBookDetailActivity( bookID );
}
}
} )
.addOnFailureListener( this, e -> {
}
);
However, the users who already have the app installed on their phone don't have this piece of code in their application which means that if they click on the dynamic link it won't do the job.
Is there any other way I can handle the dynamic link for the period until all users get the latest version?
Thank you
You could have the user copy and past the code into the app but ultimately, this also requires that the app is updated to do so.
Perhaps you could use a cloud function that would return the data in the browser rather than the app, you could do this with a button that appears after a second and manually invoke the call.
But it might just be easier to encourage users to update and implement a callback system using real-time or firestore as a user inbox system for this kind of edge case.
I am sharing data between two my app, this is my code for get the data from the shared pref in app A
try {
final Context mContext = createPackageContext("com.example.demo", Context.MODE_PRIVATE);
final String val = mContext.getSharedPreferences("pref_name",Context.MODE_PRIVATE).getString(MY_KEY,"");
Log.e("sharedtest",val);
finish();
} catch (Exception e) {
e.printStackTrace();
}
this code is inside the onCreate() method, I don't have any more code anywhere. My problem is that, if I save the some value in my app B and than start my app A the saved data were correctly retrieved at first time, after retrieving the data my activity were finishing (I have only one activity), and if I start my launcher icon and start my app A, there is no updated data(it is the same), which where changed from app B.
also if I kill my app from system app settings and launch it like first time launch updated data is here, every data change needs my app killing from settings, how can I fix that? what I'm missing?
I found solution it may be a trick but it works fine for me,
after my app A finishes its job, I'm calling the system exit method.
System.exit(1);
it makes app "A" to be exit and finish job completely
After that I had the latest updated data in my preferences
I'm working on an Android app, specifically one that uses the Facebook Android SDK. In development mode, I'm working with a test Facebook app that goes by one ID. However, in release mode, the app will be working with second Facebook app with a different ID.
I'm wondering how most Android (or Java might be a suitable enough realm of knowledge) developers here go about having their app automatically build with debug vs. release values. An ideal solution does not involve a manual switch (e.g.: switching public static final DEBUG = false; to true) before building.
It's been a while since you asked but I thought I'd share how I'm doing it.
Like Sebastian hinted, an Ant script can handle that change for you and generate the static final constants that you're looking for. You can configure IntelliJ or Eclipse to make it almost seamless.
I tried to detail the different steps I took over here, let me know if it helps. I know I never have to make any manual changes before releasing, and it's a nice relief!
In eclipse ADT 17.0 and above there is a new feature for this. Check the BuildConfig.DEBUG that is automatically built with your code.
For more information see http://developer.android.com/sdk/eclipse-adt.html
I can't recommend the IMEI method... the main problem with it is that not all Android devices will have IMEIs. A better way is to examine the signature used to sign the .apk.
// See if we're a debug or a release build
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
if (packageInfo.signatures.length>0) {
String signature = new String(packageInfo.signatures[0].toByteArray());
isReleaseBuild = !signature.contains("Android Debug");
}
} catch (NameNotFoundException e1) {
e1.printStackTrace();
}
I use a slightly more mundane method (in case you're still interested in solutions).
At application launch my application checks for the existence of a text file stored in /sdcard/. Each application I have looks for a specific file like "applicationdebug.txt". If the file exists, then the application goes into debug mode and starts being verbose with log statements and using my debug Facebook key, etc.
Then I simply remove (or rename) the file to on the device to see how the application performs in release mode.
Usually you will use 1 or 2 devices for debugging only. So you can set the DEBUG switch based on the Devices? So you can simply use the IMEI.
add a new Application class to your project and have it initialize the field (suspect to put it in a Const class).
In your Applications onCreate method, call Const.setupDebug(getApplicationContext());
Implement the setupDebug like this
public class Const {
private static boolean debug = false;
public static boolean isDebug() {
return debug;
}
private static void setDebug(boolean debug) {
Const.debug = debug;
}
private static String [] DEBUG_DEVICES = new String[] {
"000000000000000", "gfjdhsgfhjsdg" // add ur devices
};
public static void setupDebug(Context context) {
Arrays.sort(DEBUG_DEVICES);
TelephonyManager mTelephonyMgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = mTelephonyMgr.getDeviceId();
if (imei == null) imei = "000000000000000";
if(Arrays.binarySearch(DEBUG_DEVICES, imei) > -1) {
setDebug(true);
}
}
}
Switch from constant field to constant Method.
Const.isDebug()
With Eclipse I create 3 projects in the workspace :
ApplicationProject
It is a library project
Contain all source code
In values/refs.xml I add
<bool name="debug_mode">true</bool>
ApplicationProjectDEBUG
Use ApplicationProject
Overide AndroidManifest and other xml file with
developement specific config
In values/refs.xml I add
<bool name="debug_mode">true</bool>
ApplicationProjectPROD
Use ApplicationProject
Overide AndroidManifest and other xml file with
production specific config
In values/refs.xml I add
<bool name="debug_mode">false</bool>
I signe APK from this project to put on the store