I have a problem (2 problems to be exact) with launching intents from a Notification.
My current situation is that I have a family of apk that all can use a remote service launched by the first of them. The service creates an Notification and when clicked the opens back the application that launched the service. This works ok.
I wanted to improve that so when there is more than one application from the family installed, instead of just going to the apk that launched the service an chooser Intent would appear and the user would be able to choose the apk to come back to.
I managed to do this.
ArrayList<String> myApps = Lists.newArrayList(
"com.myapp1",
"com.myapp2",
"com.myapp3",
"com.myapp4"
);
List<Intent> targetedIntents = new ArrayList<Intent>();
Intent baseIntent = new Intent(Intent.ACTION_MAIN, null);
baseIntent.addCategory("android.intent.category.LAUNCHER");
final PackageManager packageManager = getApplicationContext().getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(baseIntent, 0);
for (ResolveInfo resolveInfo : list) {
String packageName = resolveInfo.activityInfo.packageName;
if (packageName != null && myApps.contains(packageName)) {
Intent targetedIntent = new Intent();
targetedIntent.setPackage(packageName);
targetedIntent.setClassName(packageName, resolveInfo.activityInfo.name);
targetedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
targetedIntents.add(targetedIntent);
}
}
Intent intent = Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=whatever")), "Select app to return to");
intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toArray(new Parcelable[]{}));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, intent.getFlags());
notification.setLatestEventInfo(this, notificationTitle, notificationMessage, contentIntent);
This works mostly as it should. The chooser appears and the selected option launches the desired apk.
But I came across 2 problems:
I. When I create the intent chooser with only the intents that interest me, the chooser is empty with the message "no application can perform this action"
intent = Intent.createChooser(targetedIntents.get(0), "Select app to return to");
intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toArray(new Parcelable[]{}));
But if I put an existing apk there first (such as google play) everything works and my options show along with the google play option.
intent = Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=whatever")), "Select app to return to");
intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toArray(new Parcelable[]{}));
This is something I can live with, but it would be better if onlye the proper apks were there.
II. When I click the apk from the list, instead of coming back from the background (if the apk was already running there) it restarts it. I have the proper flags set.
Intent targetedIntent = new Intent();
targetedIntent.setPackage(packageName);
targetedIntent.setClassName(packageName, resolveInfo.activityInfo.name);
targetedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Before using the chooser I launched only one intent and it resummed the background apk normally.
intent = new Intent(this, MainActivity.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
Not sure what I am doing wrong here that the apk restarts insted of resuming.
For the first problem (chooser issue), the reason you are getting the error "no application can perform this action" is because the Intent you are passing to getChooser() doesn't have an ACTION in it that Android can use to search for applications that can handle the Intent. getChooser will use the ACTION, CATEGORY and DATA in the Intent to search for suitable applications. It will then use package and component names to filter this list. In your case, because you've only provided the package and component names (but haven't specified ACTION), Android can't find any suitable applications. I'm not sure there's a way around this, since you want to build a list with different packages. You may just need to create your own chooser dialog (which is probably the correct thing to do here anyway, as you don't really get any advantages using the Android chooser because you've already decided what the list should contain).
For the second problem (if application is in background, it gets restarted) you need to use the following flags:
targetedIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Specifying Intent.FLAG_ACTIVITY_NEW_TASK when launching the root activity of a task will bring an already existing task to the foreground.
NOTE: If you want to just bring the existing task to the foreground (in whatever state it happens to be), then just use Intent.FLAG_ACTIVITY_NEW_TASK and remove the other 2 flags.
Related
I have an issue that my sending Activity is still visible after I kill app. How to close all Activity's when the app is killed?
DETAILED DESCRIPTION
I have a sending Intent in onDownloadStart method that displays the user a list of possible apps to "send an image", here you go:
Intent sendIntent = new Intent();
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setAction(Intent.ACTION_SEND);
File filePath = new File(String.valueOf(context.getFilesDir()));
File newFile = new File(filePath, fileName);
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".FileProvider", newFile));
sendIntent.setType(intentType);
context.startActivity(sendIntent);
This code IS working as I want with the ONE exception. The Activity with choosing the app to send the image is still visible when I kill the app. That's all - no errors in the console.
Your problem is here:
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
You start the Activity to choose an app to send in a new task. When you remove your task from the list of recent tasks (that's what "swiping your app from the recents list" does), the other task you started is still live.
Just remove Intent.FLAG_ACTIVITY_NEW_TASK from the setFlags() call and the chooser should end up running in your app's task, and therefore go away when you swipe your app's task from the list of recents.
Also, your use of the word Intent is confusing. An Intent is never visible. It is just a small bit of data used to request that Android launch a certain component. You mean Activity, not Intent.
I am using the code to call an intent in Android
Intent intent = new Intent();
String PACKAGE_NAME="com...."
intent.setPackage(PACKAGE_NAME);
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
getApplication().startActivity(intent);
Unfortunately, in some case, I do not know the PACKAGE_NAME. So, another method is using broadcast. How can I use it? Thanks all
You cant call Activity directly if you dont know package and name of that Activity.
Since you dont know package and Activity name you can only ask OS to show all possible variants for your Intent Action. In most cases user click "always open this app for this action" what means that you will directly open another app.
So in your case your code should looks like
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//dont forget to check if user has at least one application for your Intent Action
if (intent.resolveActivity(getPackageManager()) != null) {
context.startActivity(intent);
}
I have a keyguard appwidget where I'm launching a few different Intents. It seems I can launch /system apps (mms, settings etc. ) . but not /data apps ( chrome for example )
I have the following PendingIntentTemplate defined:
Intent startApplicationIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, startApplicationIntent, 0);
mainLayout.setPendingIntentTemplate(R.id.stackview, pendingIntent);
In my RemoteViewsFactory, I have the following FillInIntent:
Intent i = pm.getLaunchIntentForPackage(pkgName);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setAction(Intent.ACTION_MAIN);
stackviewRemoteView.setOnClickFillInIntent(R.id.stackview_image, i);
There are no errors, the intent just unlocks the keyguard.
It also definitely isn't null as the toString() method shows the following:
Intent { act=android.intent.action.MAIN cat=
[android.intent.category.LAUNCHER] flg=0x10000000
pkg=com.android.chrome
cmp=com.android.chrome/com.google.android.apps.chrome.Main }
Is there something special I need to do for non-system apps? I thought getLaunchIntentForPackage would give me everything I needed to launch an app the same way as it would launch if it were selected from the home screen.
Seems that the way I was doing this was making a few assumptions.
PendingIntent.getActivity calls startActivity(), which requires CATEGORY_DEFAULT to be specified.
The following code is probably 'more correct' and less likely to throw errors that CATEGORY_DEFAULT can't be found:
Intent i = new Intent();
i.setPackage(pkgName);
i.addCategory(Intent.CATEGORY_DEFAULT);
I encounter this flag - FLAG_ACTIVITY_RESET_TASK_IF_NEEDED paired with FLAG_NEW_TASK.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.applicationA", "com.applicationA.Activity1"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
This was in the following context:
1. The browser application recognized a phone number.
2. User can long press it and get a context menu with a "dial" option.
3. the browser then fires the above intent to open the dialer.
I have read the documentation for this flags over and over again, and tried playing with it, changing the activities affinities and still have no clue as to what should be the expected behavior here.
Any help is appreciated.
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.