Programmatically launch user selected application - java

So, if you haven't noticed. Many new launchers give you the option to select an application to be launched as default for a specific category.
My launcher consists of different fragments which displays cards, these cards hold a button which has a function where when the user touches the button, they're prompted with a window where they can select which application to run for that specific view. To make things easier for the user, the intent or app they selected should be saved, so if they select the button again, they don't have to select the app again.
If you would like a few examples to what I mean, take a look at 9 Cards Launcher or Smart Launcher, or any Windows 7 Phone Launcher. They prompt you with a small pop up which lets you select the application you want to run and its set.
how can I achieve this? Please I have literally searched everywhere, but nothings making sense. There are no tutorials on this, I have also decompiled various launchers to see how this works but I don't know where to start.
It'd be great if you could help.

1st of all you need to get the Installed Applications.
Then you will get the application package info from the user selected Application.
Finally launch the intent.
Here's what you can try out:
Create an intent with action=MAIN and category=LAUNCHER
Get the PackageManager from the current context using context.getPackageManager
packageManager.queryIntentActivity(<intent>, 0) where intent has category=LAUNCHER, action=MAIN or packageManager.resolveActivity(intent, 0) to get the first activity with main/launcher
Get the ActivityInfo you're interested in
From the ActivityInfo, get the packageName and name
Finally, create another intent with with category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name) and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Finally, context.startActivity(newIntent)
I would also suggest to take a look at FreeTaskManager

Related

Android - Check if activity is open for the first time

Before people say this is a duplicate of "check if android is on first run", this question is to check if the activity itself (not the app in whole) is open for the first time.
I have different activities that run Material Tap Target Prompt, so a few pop-ups that explain the buttons and functions.
But I only want it to run for first time users.
Now I tried the following:
if (prefs.getBoolean("firstRun", true)) {
prefs.edit().putBoolean("firstRun",false).apply();
........Do the pop ups
}
But this will set it for the whole app and so when the user gets to the next screen it won't run because the boolean is set to false.
So I am trying to find a way to check if the activity itself is opened for the first time but I can't seem to find anything that would solve this issue.
I thought about using a variable then setting it to 1. But if the users restarts the app, it crashes etc then that var will be reset.
May other option is to create a row in a DB and then check if that is set to 1 or whatever depending on the activity.
But maybe there is an easier way?
Thank you
Why don't you just create preference keys for each Activity. Sample code added below:
if (prefs.getBoolean(MainActivity.class.getCanonicalName(), true)) {
prefs.edit().putBoolean(MainActivity.class.getCanonicalName(),false).apply();
........Do the pop-ups
}

Android - Perform an action with voice command

Searched through the internet and couldn't find a god example of achieving the following. There is a Launcher activity. User will be navigated to the launcher activity first and inside that activity , when the user says a certain word like "Start" or something like that the app should navigate the user to another activity. I've gone through a set of tutorials , and I've seen some similar questions at SO but none of them make sense to me. What will be the best approach to achieve this ?
Basically what you have to do is
build a framework/module to handle Speech Input (speech-2-text)
Depending on Input by user, perform an action

Launch the same activity for different buttons with different resource

I am new to android and was starting out by making a list of button.i want when each button click launch a same activity for them with diffrent resource such image and raw file
what is big deal here?
call same intent twice :)
refer how to start new activity here
You can the same activity/intent multiple times, just if you want the code in said activity to do something else you might want give it some extra information by using intent.putInt(<Key>, <Value>); and others

Android window-layering in java

I am java programmer, but i never put my hand on android before. I just want to clear some basic stuff that the different between programming a window app and a android app.
I know how to programm a window app that has pop up windows inside the app.
for example:
MyWindowClass m= new MyWindowClass(new java.awt.Frame(), true);
m.setVisible(true);
But i do not know how to open a new view or layer at Android.
Can someone give me some hinds.
If you just want to display a pop-up window, just use a Dialog.
If you are looking to add an entirely new layer to your application, consider starting a new Activity (you can use either Context.startActivity or Context.startActivityForResult if you are interested in returning a value (placed in a Bundle) from that Activity.
You can also have another layer by starting a new Activity with a transparent background, but there are some limitations when it comes to that such as user input not being passed to the Activity behind it.
EDIT:
If you want to have "multiple windows" and not have them lose state (unless they are closed by the system), you can also use startActivity with an Intent on which you've added the FLAG_ACTIVITY_REORDER_TO_FRONT flag.

Android specifying application list for Intent.createChooser()

I'm curious on the Intent.createChooser() method. I've done some research but haven't been able to find the specific answer I was looking for.
I have a requirement that depending on which application the user picks (either email or text) I'm to format the text differently. I'm OK with the chooser displaying other applications (I'd prefer to not remove options for users) but is there a way for my application to know which external application the user selected? From there I could maybe overload the initial Intent and send the properly formatted data.
Thank you in advance for any help.
I'm OK with the chooser displaying other applications (I'd prefer to not remove options for users) but is there a way for my application to know which external application the user selected?
No, sorry.
For that, you would need to create your own chooser dialog, using PackageManager and queryIntentActivities(). Based on what the user chooses in that dialog, you can then set the appropriate extras on the Intent, along with the selected ComponentName, and use the resulting Intent with startActivity().
(one of these days, I'll write one of these dialogs, as this question comes up a lot...)

Categories

Resources