How to programatically clear a diffrent apps data [duplicate] - java

This question already has answers here:
How to programmatically clear application data
(10 answers)
Closed 8 years ago.
am making a app that clears the internet cache when you click a button. In which i want it to have the permission you clear other app data. I have tried many different codes and cant find a one that is used for this purpose. Is there a special permission i need or is it even possible.
I have tried this code in MainActivity.java but cant seem to get it to work:
private void clearPreferences() {
try {
// clearing app data
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear com.android.browser");
} catch (Exception e) {
e.printStackTrace();
}
}
Any help would be great. Thanks!

To clear it you simply delete the contents of it's data directory. There are 2 locations one on the SD, and another internally. You can clear the one on SD no problem, just use Apache FileUtils from Apache Commons, it has a function to delete a directory recursively. The internal dir requires root access. Or to be of same user. If you made both apps then it should be no problem just make sure they are the same user by setting sharedUserId parameter. If not, then your app could only work as root. See this answer to get data directory.
An alternative to rooting, you can open up the app info page for the app in question and tell the user to clear the app data manually by tapping on the clear data & clear cache buttons.

Related

java program which delete file and recover it [duplicate]

This question already has answers here:
How to recover deleted files using Java? [closed]
(2 answers)
Closed 4 years ago.
This is my code to delete '.txt' files from specific folder(/home/user). But once it is deleted I don't know how to recover these deleted files.
Please show me some example code (if possible), to achieve this.
Is it possible in java? If so then please help me (I am happy to use any other language.)
import java.*;
import java.io.File;
class CountFiles
{
public static void main(String args[])
{
String dirPath="/home/user";
File f = new File(dirPath);
File[] files = f.listFiles();
int s=files.length;
System.out.println("Number of files in folder "+s);
for(int i=0;i <s; i++)
{
if (files[i].isFile())
{
String FilesName = files[i].getName();
System.out.println(FilesName);
if (FilesName.endsWith(".txt") || FilesName.endsWith(".TXT"))
{
boolean success = files[i].delete();
vif (!success) throw new IllegalArgumentException("Delete: deletion failed");
else System.out.println("file deleted");
}
System.out.println("file deleted out side.....");
}
}
}
}
Yes, it is possible in Java, however for low level or OS specific tasks it may be better to use a native language.
The below info applies to both Java and other languages:
If you have control of the files before they are deleted then you can simply make a copy/backup of the file before you delete it.
If you want to recover data from a system trash/recycle bin then you need a specific solution for each OS.
However, if you do are not able to backup the files first, and do not have access to a trash folder, then you can NEVER EVER be 100% sure that you can recover the data (See edit below for more details). You can however read raw data from the storage device. This is an incredibly complex and advanced subject. If you have to ask the question then you should not be trying to write code to do it without first doing a lot of your own research.
Before reading on, refer to this answer showing how you can read raw bytes from a storage device: How to recover deleted files using Java?
After reading the accepted answer you also need to consider:
You can access the drive sector by sector, but you will have to
interpret the data differently for different file systems (FAT, NTFS,
HPFS, and more)
You cannot use a file path to get a deleted file, you need to scan the whole drive and make an educated guess at what to recover,
or ask for user input so they can choose what to recover.
The task can be a long and complex one, and you have to interpret the raw data to see if it is a valid plain text file, as well as finding it's start and end.
Edit to include the comment from Bill:
Once a file has been deleted, any further action that the computer takes (Including using recovery tools) has the potential to write over the data that you want to recover. The best solution is to force shutdown the PC, and clone the drive so that data recovery can be done on another PC using the clone while keeping the original safe.

Restrict the If block [duplicate]

This question already has answers here:
Run a piece of code only once when an application is installed [duplicate]
(4 answers)
Closed 5 years ago.
I have an if condition in my Java code in Android and I want to enter the block it's the first time the user has run the app. But for the second time and thereafter, they ignore the conditions.
How can I restrict the if block in my Java code?
This is my if block condition:
if(AppSharedPrefs.getInstance(SplashActivity.this)
.readPrefs‌​(SplashActivity.this‌​, "language").isEmpty())
{
AppSharedPrefs.getInstance(SplashActivity.this)
.writePrefs(S‌​plashActivity.this, "language","English");
}
You need to have shared preference for your purpose. On first time store value true to shared preference and when next time user comes to the app get value from shared preference if that is true you dont need to call your code
"Simple": you pick one of the options for storing data on the device.
And then, your code reads that data back every time the application starts.
See here for an overview of options, and there for the option you should pick (shared preferences).
In other words: the whole idea is that you have to enable yourself to store data; so that your code can later make decisions based on that data. That is all there is to this.

Android Variables in Background

I'd like to know how I can set a variable that will stay the same after I have closed the app. In this case I don't want to make it with a SharedPreference or a DataBase.
Thanks in advance.
If the app gets closed any variable will be long gone on the next start.
The only way to keep data is to use persistence of some sort, and the most commonly used one is SharedPreferences.
You can alternatively write to a file, send your data to a server (and load it again on the next start), or use a database.
You can also make use of a Service which you keep running in the background, that keeps your values. But you will have no guarantee about when / how the system might stop it, and they would be lost—again—like before.
If you want to keep some value, you need to persist it.
Variables endure for the life cycle of an app. When user closes an app and that app is not running a service in the background everything is deleted. In some occurences(but this has a slight chance) static variables from previous session can be read incidentally if app is restarted again, but this is not a correct behaviour.
There are 3 ways to keep your data.
Write to file: You can create files in txt, json or in any other format you wish, and read from these files on runtime to get values from previous session. I don't prefer writing to file to keep data very much. If you don't know how to use database and want mess with it, you can use this.
Shared Preferences: This is generally for saving settings file with name and value pairs.
Writing to a database: You write your data to database. SQLite and Realm databases are the most popular ones.

JavaFx Do something on first load [duplicate]

This question already has answers here:
Detect first time user in java app
(3 answers)
Closed 6 years ago.
I would like to pop up a window to select a file location when the user launch the software for the first time. I'm new to javafx and I looked for an answer on the web but no success...
Thanks in advance
I think I found it.
primaryStage.setOnShowing(event -> {
//Code here
});
It does action on first startup. I don't know if it's the best way to do it but that's how I did it. I already have a file with about 3 lines. I just added a 4th one with a random word and when the user launch the app it check if the word exist in the file. If so, it does nothing. If not, it ask the user to select the folder and if the selection is successful, it write the word.
Firstly, I would want to point out that your phrasing probably isn't clear enough for most people to understand exactly what you need. I am going to assume you have some kind of settings (like default application storage directory) which you need the user to specify at the first time the JAR is run. If the JAR file is run subsequently, it should not prompt for that again and use the settings previously specified.
Typically, when the user runs the JAR file, all the data would be isolated within that session. If the user closes the application and opens the application again, it would behave just like the previous run.
If you need to persist these data or settings, you can use Properties. This will save data in a separate file. The normal convention is to save it in the same folder as the JAR file, and named as config.properties.
At the start of the application, you should check if this file exists, if it does not exist, it means that this is the first run. Subsequently, when the user set the data (e.g. file folder), you would save it to the file.
You can find an example here.
Background
In order to implement a file selector, we can make use of JavaFX’s FileChooser. This will open a window giving us the opportunity to select a file.
What you’re asking for is for a FileChooser to open prior to entering the actual application. Let’s have a look at the implementation for something like that!
Implementation
At first, we’re going to need a JavaFX Application class that will open a window if we were to create a new instance out of it:
public class App extends Application
{
private final File file;
public App(File file)
{
this.file = file;
// Optionally provide ‘launch’ with some arguments
Application.launch();
}
#Override
public void start(final Stage stage)
{
// ...
stage.setScene(new Scene(insertNodeHere));
}
}
As I’m certain you’re already aware of—a class like this will open up a new window. This is the separate application class we’ll be calling once we’ve retrieved a File using the FileChooser in our main class.
In our main class, we’ll put this:
File file = fileChooser.showOpenDialog(stage);
if (file != null)
new App(file);
This will launch your application if the selected file didn’t turn out as null.
Moreover
With the implementation above in mind, you can complicate things as much as you feel like. Perhaps you’d like the application to start even if the file were null? In that case, there’s no need for the if statement.

Auto open application only once [duplicate]

This question already has answers here:
Log in screen on first start
(4 answers)
Closed 10 years ago.
I want to make an app similar to SetupWizard. My app should initialize ONLY ONCE when the phone is turned on for the first time and after that it is never displayed. I got the way to autorun it on every boot as a service but how to make it run only once?
I HAVE RESEARCHED
You can store some value in SharedPreferences. And place a check on the value, if it exists and, then it means the app has already initialized.
SharedPreferences can be cleared by the user(just like web-session). If you want to ensure that your solution works even if user clears his phone, then you can get the device IMEI(example here) and send it to some server, which checks whether this device has already initialized or not?

Categories

Resources