Launch an application (choosing from installed on the device) using intent - android

I know how to launch an application using an intent and the application's package name.
Like this:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
if (launchIntent != null) {
startActivity(launchIntent);
}
What I want to do is that the user could choose from the applications installed on his/her device, not a specific one.
How can I do so?
Also, I would like to open the other app inside a floating window (maybe in custom alert dialog or equivalent if there is a possibility to do so)

How can I do so?
Use PackageManager and queryIntentActivities() to find all the activities that respond to the standard home screen launcher Intent structure (ACTION_MAIN and CATEGORY_LAUNCHER). Present those to the user (e.g., in a list). See this sample app.
I would like to open the other app inside a floating window (maybe in custom alert dialog or equivalent if there is a possibility to do so)
You would need to implement your own mobile operating system. Even Android 7.0's multi-window support does not support this, except in cases where the device is already in freeform multi-window mode (e.g., Chrome OS). You are welcome to use FLAG_ACTIVITY_NEW_TASK to launch the activity into another task, which will give it a separate window on freeform multi-window devices.

Related

Changing default behaviour of wifi icon - Android

I have an app idea and have a simple question, can I change the behaviour of wifi item on quick settings menu on Android? I want it to redirect to my app. Is this possible?
Not generally.
You can examine Logcat and see what happens when the user taps that tile. If the tile starts an activity (probably yes) and the Intent that is used is implicit (action but no ComponentName or package), then you could create an activity with a matching <intent-filter>. The user would be able to choose to launch your activity instead of the standard one.
However, bear in mind that there is no requirement that the Intent that the tile uses would be the same across Android versions and device manufacturers.

How can I launch a Samsung home screen shortcut via Tasker?

My security company's mobile app (CPI inTouch) allows me to create "scenes" which trigger multiple actions at the same time with a press of one button (e.g. disarming the alarm and unlocking the smart lock on my door). I've also added shortcuts to each of the scenes on my home screen (on my Samsung Galaxy Note9, if that's relevant) so I can launch a scene via the shortcut without having to go into the app itself.
However, having just purchased Tasker, I'd like to trigger the shortcut (or the activity in the app itself) programmatically in a Tasker profile or some other automation app. I've tried plenty of apps that allow you to trigger a shortcut based on an event, but none have these particular shortcuts in the list of available ones. I read that I should be able to use a Send Intent task in Tasker to do this, but I don't know what the intent string would be or where to find it.
In case you can suggest a different way to achieve this, the main use case so far is to have my alarm system automatically disarmed when my phone's morning alarm goes off.
First thing to check would be...
create new task to Launch App,
find your CPI app and if it has a "+" in top right corner of app icon you can long press it to reveal a list of activities.
if your app does not offer those options...
The Tasker dev also has a group of plugins under the AutoApps group, there is one called AutoShortcut that launches shortcuts.
edit: there are apps such as APK Analyzer that can show you the activities and services inside an app and whether or not they can receive intents from external apps.

how to simulate a button click on an app icon/shortcut

I wonder how it is possible to simulate a button click to launch an app through its shortcut? I have seen automation apps such as Automagic (perhaps also Tasker) do this.
Are they using AccessibilityServices? If yes, how would I call such a shortcut -do I use "performGlobalAction()"?
Edit:
What I really after is to emulate/simulate a click on a button or a shortcut which apps such as Tasker can do. For instance, Google Assistant cannot be opened programatically (see How to start Google Assistant programatically?) due to permission denail. However, it can be opened by clicking on a shortcut on the screen opened according to the following instructions: https://www.ytechb.com/how-to-get-google-assistant-on-any-android-lollipop-device-without-root/
With Tasker I can then add an action which opens this shortcut. Now I am wondering how Tasker does this programatically.
You can launch another android app by using an Intent. To get the launch intent for another application you can use the
Context.getPackageManager().getLaunchIntentForPackage(String packagename);
If this is not null, then the app is installed on the device and you can launch it. If you want to get a list of other apps on the device you can use
Context.getPackageManager().queryIntentActivities(Intent intent, int flags);
You can find more information of these by looking at the PackageManager Docs

How to set Default action in Android from coding?

Once my Home Screen app 'ABC' is installed on the device & when the user presses the Home Button, he is prompted with the Default Action Android dialog to choose between the Default Home & my ABC app.
My use case is to keep on prompting the user - a bit infrequently though, with this Default Action dialog till he selects my App as default.
How we can default action in android ?
Before ICS (and maybe Honeycomb), you could set the preferred activity using the PackageManager#addPreferredActivity method.
But as stated in the documentation :
This is a protected API that should not have been available to third party applications. It is the platform's responsibility for assigning preferred activities and this can not be directly modified.
This indeed could be used by Malware to change what applications the launcher icons start. I would really advise you not to use it and instead let your users the choice to make themselves your launcher their default launcher.
call the below intent when you need to see the 'launcher' selection dialog, unless one of the launcher apps is set as default
Intent homeIntent = new Intent("android.intent.action.MAIN");
homeIntent.addCategory("android.intent.category.HOME");
startActivity(homeIntent);

Launching Android app, within an app?

I'm writing a basic application. One of the features I'm interested in trying to do is to launch another app, INSIDE the app already running.
Eg. I have an app with 3 menu options, 1 and 2 do certain tasks as part of this parent app, menu option 3 launches another app that's installed on the phone.
I'm not sure if this is possible?
This is possible with the Intents mechanism.
The exact Intent you'll have to write depend on various factors:
do you have to provide some data to the launched application?
do you target a specific application or do you want to let the user choose the application he prefers for that task (in case he has several applications able to do what you need)?
do you want to ensure that the application is available? (that would be better)
do you know if the other app provides specific intent-filters to do some tasks?
Edit:
Then, you should be able to start the second application with the following code:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setPackage("com.otherapp.package");
startActivity(i);
Place this code in the OnClickListener of your button and that should be enough.

Categories

Resources