Send intent from listview to another activity - android

I am working on an app where I am useing a PackageManager to import all the package names on the device using this code:
protected void onListItemClick(ListView l, View v, int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
}
This code manage to start the app from a ListView. But I want to send the app details to another class to start it from there instead of starting it from this class. Then I want the result to be saved but changeable by clicking on another app later.
Are there any ways of doing this?

If you only want to select the app from the list and start it using a separate button click, then how about storing the activity.applicationInfo.packageName and activity.name to SharedPreferences once list item is selected. In case the user select other item from the list overwrite the SharedPreferences parameters.
When the start button is clicked read these parameters and launch the app.

How about binding to a service, sending a message to that service, and the service will start the activity?
Or registering a broadcast receiver, sending an intent to that receiver, with the requested application as extra information, and having the broadcast receiver do the work?

Related

How do I bring my activity to the front using broadcastReceiver onReceive()?

All of this has to do with my own app.
I have two cases. First, my activity is on top, and everything works fine. My service broadcasts info and my activity updates its GUI.
My problem is that I don't know how to bring my activity to the front if its not already there. Ideally, all the activities in front of it would be closed and this one be brought to the front. How do i do this?
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//what code do I put here to bring this activity to the front and close all other activities on top of it?
}
};
Here is how I solved the problem with the help of the post bellow.
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setClass(MyClass.this, MyClass.class);
startActivity(i);
First of all there isn't a direct command to bring an activity to front such as myActivity.bringToFront. Instead you will have to let android do it for you through the intents mechanism.
So if you want to bring an activity to front you will have to call startActivity and pass an intent with the correct flags. For example the flag FLAG_ACTIVITY_CLEAR_TOP will do your job. However there is a catch, you cannot set this flag if you use startActivity from within a broadcast receiver or a service. So in your case I think that you should use the flag FLAG_ACTIVITY_NEW_TASK.
Here is an example:
Intent i = null;
i = new Intent(context, My_activity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Hope this helps...

Open third party app from intent

I am working on an app were I am going to open other apps. The only problem is that I do not know how to refer to third party apps. I am planing to use an intent. Can you refer to it useing only the packagename, or do you need the Main Activity intent. Are there any simple ways of finding the right intent, and then refer to it.
I am working on an app were I am going to open other apps.
I am interpreting this as meaning that you are creating a launcher, akin to the ones found on home screens.
Can you refer to it useing only the packagename, or do you need the Main Activity intent.
Launchers use an ACTION_MAIN/CATEGORY_LAUNCHER Intent.
Are there any simple ways of finding the right intent, and then refer to it.
Use PackageManager to find all the possible ACTION_MAIN/CATEGORY_LAUNCHER activities on the device, and then display those to the user to choose from. You can then construct a suitable Intent for starting up their specific choice.
Here is a sample project that implements a launcher.
To come up with the list of things that could be launched, that sample app uses:
PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);
And here is the actual launching logic, based upon the user clicking on one of those "launchables" in a ListActivity:
#Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
}

Android Launch foreign application from my own

I am trying to get the following code to execute:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setComponent(new ComponentName(" **Home package** "," **Home class** "));
startActivity(intent);
Essentially I am looking for a way to specifically target and load the exact, original, home application.
Technically, you have no way of always knowing "the exact, original, home application".
You can use PackageManager and queryIntentActivities() to find find who all responds to MAIN/HOME Intents. If there are two answers, and yours is one (which I am guessing is your situation), then the other is "the exact, original, home application" pretty much by definition. You can further verify this by getting to the ApplicationInfo object associated with the resolved activity and checking for FLAG_SYSTEM to see if it is installed in the system image. This approach is probably not completely bulletproof, but it may be close enough for your needs.
Yet another option is for you to simply record the current default MAIN/HOME activity when you are run for the first time. Odds are decent that your application will be run before the user elects to make you the default. Again, this has holes (e.g., they make you the default before running you for the first time).
EDIT: SOLUTION:
PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);
Collections.sort(launchables,
new ResolveInfo.DisplayNameComparator(pm));
int launcher_flag = findLauncherApp(launchables);
ResolveInfo launchable = launchables.get(launcher_flag);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
Where findLaucherApp() turns the List into an array of strings and interrogates each one to see if it contains "com.android.launcher2" and then returns its id.

Send application to back & bring to front

I want to develop an application where it will start on particular time & close on particular time. Similar like Alarm but not alarm application.
As per my thinking I will start an service when application first started service will check current time & particular timing to match the condition & when condition is to close application it will simply send application to background so that service will be running & when condition for wake up occurs service will bring application front.
Is this possible? If yes please give an example links or anything helpful.
Also I am trying to understand the service but it's little bit complex for me so if you have link which will help me to understand the service it will be very helpful. Thank You.
Problem Solved:
/*To close the activity/
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
MainActivity.this.finish();
OR Just finish activity;
MainActivity.this.finish();
/** To start the activity*/
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, MainActivity.class);
i.setComponent(cn);
startActivity(i);
It worked for me still I if any one have better solution please post below.
Thank You
You can create a BroadcastReceiver to listen for time changes, and at your specified times, create an Intent to launch or to close your main Activity.

How to bring my application activity if another application is in visible?

Consider am having 2 applications named first and second. The first applications has activity A the second applications has activity B. Initially Activity A of first application is launched and later second application will be launched from the activity A of the first application. Activity A of the first application has a count down timer. Once the timer hits How can i bring the activity A, being the application second is in visible ?
Thanks in advance.
Are you sure you need them to be different applications? Couldn't they just be activities in the same applications?
That said, you could just use an intent and start your activity that has an intent-filter for that intent?
hi try something like below code
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.urbanairship.airmail", "com.urbanairship.airmail.MainListActivity");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);

Categories

Resources