For a project I want to launch a specific application that will be installed on the android tablet.
It's not an application we have any control over (it's entirely third party) and I require a workflow for it similar to the way the Image_Capture Camera Intent works (ie, launch it, wait for it to finish and recieve the data in the onActivityResult function)
While I can launch the application using the following code:
Intent intent = this.getPackageManager().getLaunchIntentForPackage("my.thirdparty.package");
if(intent != null) {
startActivityForResult(intent,MY_INT_FLAG);
} else {
new AlertDialog.Builder(this).setMessage("This device does not appear to have the application installed").setPositiveButton("Okay",null).create().show();
}
it passes a resultCode of 0 (RESULT_CANCELLED) to the onActivityResult straight away since it's just launching the application.
What'd I'd like is to be able to discover a list of intents for a given package. There is a function in PackageManager called getInstalledApplications (which is how I got the package name in the first place) however, the flag PackageManager.GET_INTENT_FILTERS doesn't seem to populate the metaData property of any of the resulting ApplicationInfo objects.
Does anyone have any insight into either getting the intent list, or mining the data out of the APK?
Related
How to inspect an intent to see if it was an intent that started/launched the app versus an intent that was used to navigate from within the app or once the app was already opened?
We have special UI handling based on if the intent was launched using a deeplink that doesn't work well if the app is launched from a deeplink. Right now, I am adding an extra boolean to the intent to workaround the issue we have, but was wondering if there was something available in the framework/SDK to determine if a particular intent was the app start/launch intent.
NOTE: We are using jetpack navigation to handle incoming intents/deeplinks and to route deeplinks within the app. That is why in the snippet below you can see I am accessing the intent from currentBackStackEntry
Here is the code I've put in to support it so far:
In MainActivity onCreate
intent.putExtra(KEY_IS_APP_CREATED_INTENT, true)
In our UI logic code
val isAppCreationIntent = navController.currentBackStackEntry?.arguments
?.parcelable<Intent>(KEY_DEEP_LINK_INTENT)
?.getBooleanExtra(KEY_IS_APP_CREATED_INTENT, false) ?: false
I logged a bug/feature request to provide this capability here:
https://issuetracker.google.com/u/1/issues/265981498
App package name A, send intent with action ACTION_VIEW and url data to system, app B get it and open, but I just want B open intent and don't want app B get app A's package name, How to do it?
The only way to find out the package that sent the Intent seems to be via Acitvity#getReferrer() (taken from this SO post). In its docs, the following is clearly stated
Return information about who launched this activity. If the launching Intent contains an Intent.EXTRA_REFERRER, that will be returned as-is; otherwise, if known, an Intent#URI_ANDROID_APP_SCHEME referrer URI containing the package name that started the Intent will be returned. This may return null if no referrer can be identified -- it is neither explicitly specified, nor is it known which application package was involved.
Hence you can add Intent.EXTRA_REFERRER to the intents you're sending to override the default value.
Ref
https://stackoverflow.com/a/37761737
https://developer.android.com/reference/android/app/Activity#getReferrer()
https://developer.android.com/reference/android/content/Intent#EXTRA_REFERRER
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 have this code for checking app uninstall:
public void onReceive(Context context, Intent intent){
final String action = intent.getAction();
if("android.intent.action.PACKAGE_REMOVED".equals(action)){
// some action
}
Now I want get the start intent from the uninstalled app.
Is it possible?
Refer to following URLs:
Perform a task on uninstall in android
How to show an Activity BEFORE my app is uninstalled (Android)
The Post by Janusz is very helpful here..
Sadly android at the moment does not give you a possibility to perform code at the moment your app is uninstalled.
All the settings that are set via the SharedPreferences are deleted together with everything in the Aplication Data an Cache folder.
The only thing that will persist is the data that is written to the SD-Card and any changes to phone settings that are made. I don't know what happens to data that is synchronized to the contacts through your app.
I guess the only way to discover this is to test this. You can use the following code to find the launch intent of an application:
final Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
where pm - is PackageManager.
To my point of view this is impossible and you'll receive launchIntent equal to null. But you should check this on your own.
My boss asked me to prove that my application behaves properly when summoned by another application (dunno why he asked that).
So I have two apps here, one launches a second one. How I launch the specific app I want? Using Intent launch seemly any generic app that reaches a certain goal, not the app I really want.
Give this a try.
Intent secondIntent = new Intent();
secondIntent.setAction(Intent.ACTION_MAIN);
secondIntent.setClassName("com.example", "com.example.YourSecondApp");
startActivity(secondIntent);
I should point out that com.example should be the package of your second application (the one you want to call) and com.example.YourSecondapp is the class name where you have your onCreate() method.
Intent secondApp = new Intent("com.test.SecondApp");
startActivity(secondApp);
Check out for more examples
http://developer.android.com/resources/faq/commontasks.html#opennewscreen
Create one Intent using the following code
Explicit Intent
When you know the particular component(activity/service) to be loaded
Intent intent = new Intent();
intent.setClass("className/package name");
start<Activity/Service>(intent);
Imlicit Intent
When we do not have the idea which class to load and we know the Action to be perform by the launched application we can go with this intent.
Action needs to set, and the Android run time fallows the intent Resolution technique and list out(one or more components) the components to perform the action. from the list out components (if more than one), user will get the chance to launch his chosen application