notify installation complete from service - android

I have this piece of code
private void initiateInstallation() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/example.apk"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
that from within my service installs an application named example.apk
I want after the installation is finished to run an activity which notifies the user about the installation.I did that except the activity appears before the installation finishes.
The problem is that within a service I cannot use startActivityForResult. So, I need a way around this so that I can start my notification activity(or for the sake of example just print something out with Toast within the service) only AFTER the installation is complete.
I already tried some answers from other questions like "alternative to startActivityforResult in services" but still I couldn't figure this out.
I also put the code so that maybe there may be something done in there.
Thanks in advance ... any suggestions are welcome.

You could listen to the PACKAGE_ADDED broadcast intent: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED
As far as I know, these are sent after the installation is done, and you can listen to those from the service.
Just note that if the application was already installed, you will get ACTION_PACKAGE_CHANGED (as far as I know).
Also you must know the package name as well, not just the apk name, since the intent will contain the package name.

The answer given by #Pal Szasz is technically correct (as far as I know ;-) ).
However, based on the information given in your question, I assume you only wish to show a notification (no further programmatically actions are to be performed). If my assumptions are correct I would respectfully advise you NOT to show such a notification. And this is why:
The Android system already has a standard means of passing notifications to the user. The status bar will in this case already show you a message saying that the new app is successfully installed (or not installed in case of an error). If you implement yet another notification channel you will most likely confuse or irritate your users by diverging from the standard, expected behaviour.
Taking this beyond the borders of sanity one could also argue for the fact that you in some sense also would contribute to the fragmentation of Android (in a very small scale, but nevertheless).

Related

How to know if the programmatically install/update of my app is successfull

I'm currently create an application that have the possibility to updated from own code thanks to a downloaded .apk from a server.
I would like to know if the update/Install of the app is successfull. Because currently the update working fine but i don't know if the installation/update is ok or not, if the installation face a problem or if the user cancel install or somethings else. So i need to have a callback "installFinish(boolean isOk)".
i search for a day but i don't find solution for my problem.
I read lot of things, particulary this :
How to find out when an installation is completed
or this :
Android - Install Application programmatically with result
but is not what i seach.
This is my code for update my app
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file.getAbsoluteFile()), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).startActivity(intent);
EDIT
I tried this https://stackoverflow.com/questions/29175722/how-to-get-onactivityresult-called-from-action-view-intent
but it's not working for me for two reason i think:
I don't know whether the user pressed Cancel
onActivityResult is never call after updated the same app, maybe because it's the same app which updated the app who launch the intent is kill. ?
Maybe the last reason it's the same reason why i don't receive broadcastevent such as listening for ACTION_PACKAGE_ADDED in the app who launch the update.
Maybe i can call the method when the first time the application is launch, it's like install is successfully because the app is started.
Heres a work around which we use:
Implement a dummy provider and override onUpgrade().onUpgrade is called whenever database is upgraded.U will need maintain one to one mapping between database version and app version.

When you use Intent.ACTION_DELETE, how can you tell if the user actually uninstalls the app?

This is my code
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + theApp.getAppOpen()));
startActivity(intent);
When startActivity is called, a default prompt comes up, asking the user whether they want to uninstall that app.
How can I tell if the user says "ok" to uninstall the app?
Assume my app is not the one being uninstalled.
You have two options, and you might want to use a combination of the two:
1) Register a BroadcastReceiver for ACTION_PACKAGE_REMOVED, and once fired, you can inspect the data of the intent to see whether your package was removed. It might be wise to add a time-out of sorts, possibly through an Alarm set five minutes into the future.
2) Once the user returns from either uninstalling or canceling the uninstall, your Activity will resume. You can check whether the package of interest still exists in onResume(), using PackageManager.getPackageInfo() or similar. Note: The user might not return to your app, in which case the time-out/Alarm recommendation would become important.
I think you can use the "ACTION_UNINSTALL_PACKAGE" intent (API 14 and above) so that you could check if the uninstallation has succeeded or not.
docs say:
Activity Action: Launch application uninstaller.
Input: The data must be a package: URI whose scheme specific part is
the package name of the current installed package to be uninstalled.
You can optionally supply EXTRA_RETURN_RESULT.
Output: If EXTRA_RETURN_RESULT, returns whether the install succeeded.
Constant Value: "android.intent.action.UNINSTALL_PACKAGE"
It might be late but you can use startActivityForResult(removeIntent, 0)
as a first step and then after taking any action - whether it's uninstalled or not - onActivityResult method is called, so, for a second step you can check in onActivityResult whether this app now exists or not. Follow this for more details of the second step:
[Check if application is installed - Android

How to configure the android intent actions in android manifest

I have a webview in my app, on trying to do actions like making a call (Tapping call button from results displayed in webview), sending mails and other actions, my webview doesn't perform those actions
I Found a solution to add the intent actions in my web view activity as
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent)
Instead of doing so is there any way to add in the android manifest file
or Is there any way to turn on all of the intent actions for the webview so that
there wont be further issues in handling the actions
Can someone help me on this pls
Your answer seems to me a bit strange, I think you are a bit confuse about the difference between Intent and manifest permission. The first one are the system used by android to let app communicate with each other, the second one allow you to use some feature of the device like wifi and direct phone call that need the explicit agreement of the user to be used (the prompt that popup when you make the first install of an app).
With this clarification it is clear that if you want to do something that require another app you will have to make an Intent. This Intent, if well formed, will be elaborated by the os that will take care of sending it to the correct application able to accomplish the Intentrequirement.
So the answer to your question, as far as i know, is no, you have to use intent if you have the need of calling external app. It's also a good practice to set in the manifest only the permission really needed by the app, this way the user know what the app really can do and and what it can't do.
Hope i understand your question and answer it.

Android intent opposite of setComponent

I know I can force an intent to use a specific application/activity by using intent.setComponent(). But is there a way to say that it should not use a specific app/activity?
Why I need this:
My application will be able to respond to several urls (like http://www.companyname.com/something/12345). If my app launches cause of an Intent to this url, I'll check if I have the needed data to handle this. (In my example, something with id 12345). If I'm not able to do something useful with the intent, I create a new Intent with the same data, so the user can try to view the content in its browser.
This is the moment my problem occurs. When I start my newly created intent, my app appears again in the list of apps that can handle this. But I already know that my app can't do anything with this intent. The user already knows that my app can't handle the intent, but still it'd be nice if my app just didn't appear in this list.
Thanks in advance.
A possible solution that works is when creating the new intent, instead of intent.setData(uri) I do intent.setDataAndType(uri, "text/html").
This causes that my app disappears from the list. At the moment it's a solution that works :) But still I'd like to know if there are better ways to achieve this result.
You may use Intent-Filters for your app and set up the URI with wildcards as you need, see here for more information. With some additional info via an action or category, you may ensure, that your app it the only one (not) matching against the intent.

How to let user know he/she needs to install another app that my app depends on

I am developing an application which has defined some intent filters (in the form of action strings, e.g. com.example.project.UPLOAD) for other applications to use. Consider a device that didn't have my application but with applications that use my intent filters, the Intent created will fail the action test as described in the documentation. Is there any way to prevent this happening or give a better user experience? Here are some of the approaches I can think of but don't know if there are feasible:
While installing an application that depends on another applications to handle some of the intents, suggest user to install the application that can handle the intent
Dynamically determine if the intent can be handled. If not, launch the market showing the application that can handle the intent
What is the best approach to handle this? Please provide some implementation references if possible.
Aside from mentioning it in the Marketplace, I'm not sure how you'd go about presenting messages during the application installation, as (to my knowledge) there is no supported way to execute code upon installation.
If other applications use your filters, then it's up to them to make sure your package is installed. You can't really give them anything without being installed.
They can test to see if a package is installed using the PackageManager, and adjust their logic to notify the user when they need to install your package. Example:
private boolean isInstalled(){
ComponentName comp = new ComponentName("com.yourpackagestuff", "com.yourpackagestuff.TestClass");
Intent intentName = new Intent().setComponent(comp);
List <ResolveInfo> list = ctx.getPackageManager().queryIntentActivities(intentName, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
That's how I'd go about it, at least.
You can try calling the Intent and catch the ActivityNotFoundException if it is thrown. If it does get thrown, you know it doesn't exist so you can implement your backup code.
This solution is quicker Android check for dependent application during installation?
getPackageManager().getApplicationInfo("com.myproject", 0 );
No exception means its ok

Categories

Resources