Perform action after Clear Data of my application - android

I want to be notified when the user or any other application Clear the data of my application (e.g. User click on Clear data button from app info). Is there is any way to know that this action was taken.
I expect to find Broadcast intent that I can receive to handle my action! is it possible? IF not are there any other way to know or not?

I expect to find Broadcast intent that I can receive to handle my action!
There is one, but your app cannot receive it. Presumably, this is to prevent malware from trying to interfere with this process somehow. If the user wants your data to be cleared, please allow it.
IF not are there any other way to know or not?
IMHO, an app should be idempotent with respect to application data. In other words, the app should treat a fresh installation identically as it would treat starting up for the first time after its application data has been cleared, which would also be identical to the app having been uninstalled and later reinstalled. Any other behavior either has privacy ramifications (e.g., trying to use some device-specific identifier and record whether the app had been installed here before) or clutter ramifications (e.g., storing some file on common external storage and checking for its existence when your app realizes that it has no application data on internal or its portion of external storage).

Related

How to detect when app was restored using Backup Service?

In my app user can choose custom files to be used instead of some default settings. File's info is stored by its URI and I have hard time handling case of Backup Service restoring whole app, when all the restored URIs will be invalid.
App will turn to using default settings whenever it detects invalid URIs (not pointing to custom files), however I would want to inform the user about this fact after restore is performed, so he knows why app is using default settings.
Is there any way to detect when app was restored using BackupAgent?
So far I didn't find any proper tool for detecting when app was restored with use of a Backup Service, however I found a workaround:
context.packageManager.getPackageInfo(myPackageName, 0).firstInstallTime always returns time of install of the current instance of the app, so it is possible to store this time using backed-up SharedPreferences and then comparing it with the time received from packageManager - if it is "newer" than the one stored in SharedPreferences, then it means the app has been restored by a Backup Service.

How to lock (other) apps with password?

I've seen some "app locking" solutions in the Google Play store and I was wondering how those apps work. I'd like to build something similar.
I realize that this might require some special permission or maybe request the app to be added as device administrator.
Is there some broadcast that is triggered just before an app is launched that I can intercept and do some action (e.g. launch an activity that will request the user to fill a password)? I've read some lengthy discussions how this is not a good idea and the only idea is to have a background service that will continuously poll the running processes and check for changes, but I think retrieving this list every second and checking it for chances is not good for the battery and I think other app locking apps out there must be using a different approch.
If possible, without the need for a rooted phone.

Can I install an app from an other app and then launch its intent immediately when opening it?

Background: I have an app which needs to use an intent from an other app. If this other app is not installed when its intent is needed, I would like to offer it for download and install.
Question: If the downloaded app is opened (by the user) immediately after installing it, I would need it to open the specific intent the first app needs instead of opening it as normal. See image below.
What options do I have available, is there a common pattern for this? Thanks in advance.
If the downloaded app is opened (by the user) immediately after installing it, I would need it to open the specific intent the first app needs instead of opening it as normal.
That is not possible, strictly speaking. There is nothing stopping the user from pressing Open, and that will behave as normal -- you cannot change this.
If you are the author of the, um, "Monkey Trampoline" app, you could work out various hacks to recognize that it is being opened after an install from, um, "Animal Olympics", so it can route its logic accordingly (use a custom sticky broadcast, have the second app use some IPC to ask the first app "yo, am I supposed to do something special?", etc.).
It is also conceivable that ACTION_PACKAGE_ADDED will be broadcast before the user clicks either Done or Open (or HOME or BACK or whatever). In that case, you could listen for that broadcast, determine that, indeed, the app just installed does involve simian somersaults, and call startActivity(). This will be a bit jarring for the user, insofar as all of a sudden they'd be transported from the install process into this new app. And, since the precise timing of ACTION_PACKAGE_ADDED is undocumented, your mileage may vary (e.g., the user could still get a chance to tap Open before the broadcast winds its way to your app).

Start application when Content provider is changed

I want my application to start when someone modifies a content provider. A setting to be specific. The settings framework calls "notify" when a value is set.
If my app was started I would use registerContentObserver() I guess, but is is not started.
Can define some intent-filter in my manifest that wakes up my application. A back up plan would be to have a service running all the time that has registered a listener, but that seems like a wast or resources.
Thanks, Ola
This isn't directly supported by the Android device because starting an app every time a ContentProvider's data changes is a path to really killing your battery. To do the query, you'd need to do it in a service, which as you said is understandably undesirable.
Secondly, starting an intent is a user action. Android really doesn't support allowing an application to start all on its own without user request... Doing so would be impolite! What if your user was doing something important and then your app pops up on top? Remember the user is in control, not you. Instead of starting an application, consider placing a Status Bar Notification so the user can deal with it when it's convenient for them.

How to make an Android app the default for a filetype?

I need my app to be the default action for opening a particular file type. How do I control this? Also, how can my app access the file being opened?
Thanks.
Have a look at android.content.Intent.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:
action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
Your activity can obtain the Intent that launched it by calling Activity#getIntent() and then use Intent#getData() to determine the URL of the data to open.
Your manifest should specify that one of your activities can view a specific type of data using an <intent-filter> element. If the user attempts to view this kind of data, the system will figure out which activity across all apps on the system should get to handle it.
If there is only one app capable of viewing the data it will be launched automatically. If more than one app can handle the intent, the user will be presented with a dialog asking which app they would like to use, along with a checkbox for making that choice the default for next time. Users can clear defaults from the systemwide Settings.
As Aidan noted there is no way to hijack the default. The user must choose to make your app default for handling that type of data.
As far as I'm aware you can't enforce it upon the user to make YOUR application the default for opening a particular file extension. The user must choose to make your application the default. As for the second question I don't know but thought I'd provide a partial answer. :)

Categories

Resources