I am a newbie on Android and I am creating a launcher. I want apps to be removed (uninstalled) so I have a list and I invoke system to uninstall it.
How can I know if the user pressed "Cancel" or "Ok" in the system dialog?
(I know the system will unisntall the app if I press "Ok" or wont if I press "Cancel", I just need to know how to get the answer to remove or not the app of my list [ArrayList]).
If you cant know it, how can I do to remove an app from a list without knowing if the user is going to uninstall it or not?
public void uninstall (int position){
Uri package1 = Uri.parse("package:"+apps_block.get(position).name.toString());
Intent uninstall = new Intent(Intent.ACTION_DELETE, package1);
startActivity(uninstall);
AppDetail toRemove = adapter_block.getItem(position);
adapter_block.remove(toRemove);
}
With this code, the app is always removed from my list even I press "Cancel".
You are removing the item from the list immediately after startActivity(). The user has not even seen the dialog yet by that point.
You could listen for the ACTION_PACKAGE_REMOVED system broadcast, confirm that it was for your requested package, and remove the package from the list at that point. By doing this from your activity via registerReceiver(), you can find out fairly quickly and have easy access to your UI code to update the list.
Related
I'm making an app in which you can chat and call with other contacts. But in case of calling, I've designed the app in such a way that after typing the number and clicking on the call icon, it takes you to native calls app for calling and updates the call log in my current app.
For this process, this is the code I've written:
if (nativeCall(mobileNumber)) {
Intent intent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel:" + mobileNumber));
if (((BaseActivity) context).isNetworkOk()) {
addToUserCallLogs(context, DateUtils.convertTimestampToDate(), contactUri, "Out", System.currentTimeMillis());
}
context.startActivity(intent);
return true;
}
You can see that I'm putting mobile number into the intent and starting it. And I'm using addToUserCallLogs() function to show it in my app's call logs.
This works fine usually, but in the issue is in the following case.
When the user has multiple calling applications(For eg, the user has installed application named SMARTalk. Now he has native caller app and SMARTalk app to call from), in that case the Android system gives options to chose from like this:
Now, if he choses from one of them, even in that case there is no issue. Say he didn't chose any of those and clicked on the other part of the screen. Then this options bar will be closed. Since all this is happening after starting the intent, this call will be added in the call logs of the app from the function addToUserCallLogs(). But I don't want the call to be shown in the call Logs because I haven't done any call.
But according to the code I've written, before starting the intent, I'm adding into my app's call logs database. Is there a way the information of whether the call has happened or not can be sent back from the system to the app?
Or a way to get these options to be shown manually from the app?
Please comment if you need any more explanation.
I guess no way to receive the callback information because ACTION_CALL does not return a result. You can see the output is nothing from docs even you use startActivityForResult
My Android Application has an option to upgrade to the newer version, the newer version APK I keep it available under a path in sdcard. On click of Upgrade option I invoke following method.
public static void launchInstaller(Activity act, String apkPath)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.startActivityForResult(intent, 0);
}
The reason I include FLAG_ACTIVITY_NEW_TASK, is because, after upgradation, I want to have "Open" & "Done" options, which aren't shown if I don't use this flag.
When the above code launches the package installer, it has two options OK & Cancel, when user press Cancel, I want to know user cancelled it. But I am unable to know because the onActivityResult is called pre-maturely. I come to a reason why is that happening after reading the following posts.
Android - startActivityForResult immediately triggering onActivityResult
onActivityResult() called prematurely
They ask me to make sure that the Intent I am using to launch the activity doesn't have FLAG_ACTIVITY_NEW_TASK set on it. See here:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
In particular note: "This flag can not be used when the caller is
requesting a result from the activity being launched."
If the activity is being launched as part of a new task then Android
will immediately call the onActivityResult() with RESULT_CANCELED
because an activity in one task can't return results to another task,
only activities in the same task can do so.
But in my case, I can't remove FLAG_ACTIVITY_NEW_TASK, because otherwise I will not get "Open" and "Done" options on successful upgradation.
Has anybody faced similar sort of problem? Kindly help me out, as it drives me nuts.
You should use package added broadcast intent. Create a broadcast receiver listen package added.
If added package equals your package, user confirm install otherwise dismiss install process. If you ask what kind extras can be return by android, android returns EXTRA_UID about package. PackageManager' s method convert uid to package.(getPackagesForUid(int uid) returns String array but it usualy return one item in the array in short usually array length equals 1.
I use an Navigation intent in my app like:
uri= String.format(Locale.ENGLISH,"http://maps.google.com/maps?daddr=%f,%f",latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
EventMapOfflineActivity.this.startActivity(intent);
Works perfectly, the user can select the app (s)he wants to start.
But, after the selection the dialog won't close itself and after returning to the app sometimes the user has to press the back button up to 4x to close that dialog.
So is there any way to get the Android OS to close that dialog?
Or do we have to respect that Android way of doing it?
I would do that in the onStop() Handler of my app which is called when the user selected an app to start in the dialog (with a flag set to true on the click to go to navi and resettet to false in the onStop() )
I just think that the Android OS would not like messing with its dialogs, or is there a CLEAN way to do this?
Thanks in advance
But, after the selection the dialog won't close itself and after returning to the app sometimes the user has to press the back button up to 4x to close that dialog.
This is most likely a problem with your firmware, either due to bugs from the device manufacturer or bugs in some modded firmware that you have installed upon your device.
So is there any way to get the Android OS to close that dialog?
Android automatically closes the dialog.
is there a way to reprompt the user to choose a default activity for an intent? For example, user selects his default home apk and I want him to reconsider his choice once again.
I know how to do that on 2.1 and before, but is there a way to do that now on 2.2?
Famous Home Switcher, which did similar thing, does not work on 2.2 anymore thanks to google team
This is how I represent the Activity selection dialog:
It start the android default ResolverActivity for "HOME" Applications.
Intent selector = new Intent("android.intent.action.MAIN");
selector.addCategory("android.intent.category.HOME");
selector.setComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));
startActivity(selector);
The above code is working for my 2.2 enabled tablets.
When executed, it displays the "Complete Actions with:" dialog with all possible Home applications in the list.
A way to detect which is currently set by default you could ask for all preferred activities. The lists "filters" and "comps" contain the data when calling .getPreferredActivities(...).
filters - contains the intent filter data, which you could query what type of data it is.
comps - contians the component which would be called if the intent filter matches
This way you could check if your application is the current "home" application set as preferred by the user.
List<IntentFilter> filters = new ArrayList<IntentFilter>();
List<ComponentName> comps= new ArrayList<ComponentName>();
getPackageManager().getPreferredActivities(filters, comps, null);
For example, user selects his default home apk and I want him to reconsider his choice once again.
That is no longer possible, unless your app is the preferred one. Then, I think you can use clearPackagePreferredActivities() to remove yourself as the preferred choice.
In other words, you are welcome to affect your own app, but you are not welcome to affect other apps.
I'm writing a program for the Android Platform and I would like to implement the code of a preexisting application found here .
There is a button in my application menu that says "Show Friends on Map" so I want this program to start from the button press.
For greater detail I will give a small diagram.
User Starts My application > User Presses "Menu" Key > User Presses "Show Friends on Map" > WAMF.apk (the application in the link above) is launched
Is there any way I can do this?
If I understand you correctly and all you want to do is launch WAMF, see this blog post.
In it is the following code, which will detect whether the OpenTable (or WAMF, in this question) is installed, and if so invoke it, otherwise take the user to the Android Market to download OpenTable:
public void showReserveButton() {
// setup the Intent to call OpenTable
Uri reserveUri = Uri.parse(String.format( "reserve://opentable.com/%s?refId=5449",
opentableId));
Intent opentableIntent = new Intent("com.opentable.action.RESERVE", reserveUri);
// setup the Intent to deep link into Android Market
Uri marketUri = Uri.parse("market://search?q=pname:com.opentable");
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
opentableButton.setVisibility(opentableId > 0 ? View.VISIBLE : View.GONE);
opentableButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
PackageManager pm = getPackageManager();
startActivity(pm.queryIntentActivities(opentableIntent, 0).size() == 0 ?
opentableIntent : marketIntent);
}
});
}
As commonsware says, this is assuming that WAMF is available in the Android market. If not, you're out of luck.
(I'm hoping Reto Meier sees your question, as WAMF is his app)
Well, as I see it, you have two main choices.
Option #1 says that WAMF is installed as a separate application. That may be tricky, as it is unclear if this application is available for distribution anywhere (e.g., Android Market). But, assuming it is, and assuming the user has the app installed, when the user invokes your desired menu choice, you need to call startActivity(), using an Intent that will resolve to whatever in WAMF you would like to have displayed. You can also use PackageManager to detect if WAMF is installed (i.e., seeing if there are any activities that would match the Intent you want to use in startActivity()) -- that way, you can disable the menu choice, or have it pop up a dialog telling people to install WAMF, or something.
Option #2 says that, since WAMF is Free Software, you simply integrate the relevant portions of code straight into your app. On the plus side, there's no question whether the code is there. However, should Mr. Meier update the year-old WAMF, you would have to re-integrate his changes. Also, his application is released under GPLv3, which may or may not work with your own app's licensing scheme.