How to find data usage of newly installed application in Android - android

How to find data usage of newly installed application?
getUidTxBytes(int uid) and getUidRxBytes(int uid) are used to get the data usage, but in case if a new app is installed and using the data, how can I identify that the new app is installed? And also how come I know the newly installed app getting data usage?
I am pretty new to this topic.

You can register for Broadcast Intents from the system like ACTION_PACKAGE_ADDED and ACTION_PACKAGE_CHANGED to be notified when new applications are installed or updgraded. The UID of the package is always passed with these Intents as an extra.
SDK Docs link for more information.
HTH

Related

Callback for programatically install .apk

I update my app using a local downloaded .apk, via DownloadManager, using this code:
val installIntent = Intent(Intent.ACTION_VIEW)
installIntent.setDataAndType(localUri, "application/vnd.android.package-archive")
installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
this.startActivity(installIntent)
Although most of the time it works, sometimes, only some of the users get the "There was a problem parsing the package error" popup.
I'm interested if there is any callback that we can get in our app, whenever this happens, or user press 'OK' in the popoup . I found here for instance, a callback that checks if the package exists (meaning that it was installed) but I'm more interested for updates, in this case I already know that the app was previously installed.
Is there a callback (maybe startActivityForResults) for this specific intent, so we know that it was successfully installed or it failed?
ACTION_VIEW is not designed to return a result. On Android 5.0 and higher, PackageInstaller gives you a direct API for installing apps, where you can provide a callback (in the form of an IntentSender) to find out about how the installation proceeds.
This sample project shows the basics of using PackageInstaller, and you should be able to find open source apps that use it in a more sophisticated fashion. For example, the F-Droid client probably uses PackageInstaller.

When will android broadcasts intent ACTION_PACKAGE_NEEDS_VERIFICATION?

About ACTION_PACKAGE_NEEDS_VERIFICATION intent, the android docs says:
`Sent to the system package verifier when a package needs to be verified. The data contains the package URI.
This is a protected intent that can only be sent by the system.`
Found at https://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_NEEDS_VERIFICATION
But what kind of verification does it means? In what kinds of occasions or scenarios will android broadcast this intent?
Looking at the recent source code (around line 10669), it appears that the system will broadcast this intent during the installation of a new package if there are any package verifier installed:
/*
* Determine if we have any installed package verifiers. If we
* do, then we'll defer to them to verify the packages.
*/
As for an explanation of what package verifiers are and what they do, my understanding is that they will need to have the PACKAGE_VERIFICATION_AGENT permission (reserved for system apps) which gives them the ability to respond to the broadcast to accept or reject the installation of the new package. The Google Play Store app is a verification agent that appears to use Google's online app verification service to perform this function.
Here is a page that has a lot of interesting information on the subject (along with several other pages that comes up if you do a "android package verifier" search online).

Is there an intent for uninstallation of an app for ALL users?

Background
The normal way to call for the uninstallation an app is simply by using the "ACTION_DELETE" intent :
startActivity(new Intent(Intent.ACTION_DELETE, Uri.parse("package:" +packageName)));
The problem
starting with some Android version (don't remember which) , apps can be installed for multiple users on the same device.
This means there is a new way to uninstall an app, one which will uninstall it for all users (image taken from Lollipop - Android 5.0 ) :
The question
I've searched in the documentation, but couldn't find the answer those questions:
Is there any way to perform this operation via an intent? Maybe something to add to the intent I've written above ?
Does ADB have a new command to remove an app for all users?
Is there a way to check if an app is installed for multiple users?
Is there any way to perform this operation via an intent? Maybe
something to add to the intent I've written above ?
Yes, but be careful. You can pass in Intent.EXTRA_UNINSTALL_ALL_USERS.
However, it's hidden because it:
should not be part of normal application flow
You could just pass in the constant anyway, if you feel it's necessary and disagree with Google on that one. Just for example, here are the differences between passing in false and true with that constant
final Uri packageURI = Uri.parse("package:" + "some.package.name");
final Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
uninstallIntent.putExtra("android.intent.extra.UNINSTALL_ALL_USERS", false or true);
startActivity(uninstallIntent);
Results
Does ADB have a new command to remove an app for all users?
No, the command remains the same.
`adb uninstall 'some.package.name'`
This will remove that app for all users. I'm unaware of a way to specify a particular user.
Is there a way to check if an app is installed for multiple users?
No, not that I'm aware of. In fact, when the Settings apps decides to place the "Uninstall for all users" option in the options menu, it's basically doing so based on whether or not there are multiple users period, not if both the current user and another user have an app installed.
Not to mention, most of the methods in UserManager that you'd need to even tell if there are multiple users on the device, like UserManager.getUserCount, require the MANAGE_USERS permission which is a system API and hidden. So, I'm not even sure why that's a public method.
Also, you can easily test all of your questions, much like I did, by creating a dummy user on your device. You don't even need to log into a Google account.

Why does a ParseUser have been registered without a deviceToken?

I've experienced a strange thing in my Parse app: a new Installation record has been added, from an Android device, but its deviceToken field is blank. Probably for this, the new user doesn't receive any push notification (subscribers targeting his device are always zero).
Why this situation?
You may also use installation table just to track installation of app across user devices, for example to handle loading data from other device ...

Tell if running AOSP or Google Android?

I've searched and cannot find this, though mainly a challenge of knowing what to search for, I'm sure it's been asked before.
How does an app deduce whether it is running on a "Google Android" device, or an AOSP device (e.g. Kindle Fire etc)?
You can try to enumerate an existing accounts on device using AccountManager class: getAccountsByType(), passing com.google as desired account type. If there is no accounts of this type, this mean either it is AOSP device or the user didn't create Google account yet.
If it is not enough for you, you can use PackageManager class and query some Google-specific package using getPackageInfo() method. For example, com.android.vending - Google Play app.
But pay attention that no one of these methods can guaranty you that target device is running AOSP.
This is not trivial, but most apps do not need to even care. If you do, then it usually means you need some unique features therefore it's simpler to check if certain feature is present or not, instead of what label is on the device. You may also check for presence of certain packages (like Google Play), but lack of it does not mean automatically it is i.e. Fire.
You can check whether you can open market urls by using an Intent.
Documentation on the intent's extras is here.
The Intent would look like this:
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
//you can use any package identifier for the check.
startActivity(intent);
} catch (ActvitiyNotFoundException anfe) {
//There's no market installed.
//So you can guess that you're not on a device with Google experience
}
The downside of this method would be, that the user will be taken to the Play Store if it's available.

Categories

Resources