I am launching the camera app in normal or secure mode depending on what gesture is performed using my app but once the user selects the app and taps Always then there is no option to change the defaults, even from the settings menu in Android.
camera_intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
secure_camera_intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE);
camera_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
secure_camera_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
this.startActivity(camera_intent);
//this.startActivity(secure_camera_intent);
Is there a workaround? I want to show the camera selection dialog once again so that the user can change his choice.
If you want to present the app chooser even if the user has a default activity selected for the task, use Intent.createChooser().
From the Android developer guide:
If you call Intent.createChooser(), passing it your Intent object, it
returns a version of your intent that will always display the chooser.
This has some advantages:
Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
If no applications match, Android displays a system message.
You can specify a title for the chooser dialog.
You should also check the documentation for ACTION_CHOOSER for guidelines on where this is appropriate or not (Intent.createChooser() is just a convenience method for this).
startActivity(Intent.createChooser(senderIntent, "Title here"));
It will always display the chooser dialog.
Related
I am trying to share a link from my app with direct share. The share dialog must be like the image below with the most used contacts from messaging apps, like WhatsApp contacts.
This is the Intent structure which I am using for share the link:
Intent shareIntent = ShareCompat.IntentBuilder
.from(getActivity())
.setType("text/plain")
.setText(sTitle+ "\n" + urlPost)
.getIntent();
if (shareIntent.resolveActivity(
getActivity().getPackageManager()) != null)
startActivity(shareIntent);
And this is what my app shows:
Any idea how to achieve that?
You should use .createChooserIntent() instead of .getIntent()
Like this code below, you can use Intent.createChooser
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file://" + filePath);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
You should use .createChooserIntent() instead of .getIntent()
Docs: This uses the ACTION_CHOOSER intent, which shows
an activity chooser, allowing the user to pick what they want to before proceeding. This can be used as an alternative to the standard activity picker that is displayed by the system when you try to start an activity with multiple possible matches, with these differences in behavior:
You can specify the title that will appear in the activity chooser.
The user does not have the option to make one of the matching activities a preferred activity, and all possible activities will
always be shown even if one of them is currently marked as the
preferred activity.
This action should be used when the user will naturally expect to
select an activity in order to proceed. An example if when not to use
it is when the user clicks on a "mailto:" link. They would naturally
expect to go directly to their mail app, so startActivity() should be
called directly: it will either launch the current preferred app, or
put up a dialog allowing the user to pick an app to use and optionally
marking that as preferred.
In contrast, if the user is selecting a menu item to send a picture
they are viewing to someone else, there are many different things they
may want to do at this point: send it through e-mail, upload it to a
web service, etc. In this case the CHOOSER action should be used, to
always present to the user a list of the things they can do, with a
nice title given by the caller such as "Send this photo with:".
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
I know there have been a lot of posts related to this:
How to know which application the user chose when using an intent chooser?
https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1
How to get the user selection from startActivityForResult(Intent.createChooser(fileIntent, "Open file using..."), APP_PICKED);?
Capturing and intercepting ACTION_SEND intents on Android
but these posts are somewhat old, and I am hoping that there might be some new developments.
I am trying to implement a share action without having it be present in the menu. The closest solution to what I want is provided by ClickClickClack who suggest implementing a custom app chooser, but that seems heavy handed. Plus, it seems like there might be some Android hooks to get the chosen app, like the ActivityChooserModel.OnChooseActivityListener.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage());
sendIntent.setType("text/plain");
Intent intent = Intent.createChooser(sendIntent, getResources().getText(R.string.share_prompt));
ShareActionProvider sap = new ShareActionProvider(this);
sap.setShareIntent(sendIntent);
sap.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
#Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
System.out.println("Success!!");
return false;
}
});
startActivityForResult(intent, 1);
As of API level 22 it is now actually possible. In Android 5.1 a method (createChooser (Intent target, CharSequence title, IntentSender sender)) was added that allows for receiving the results of the user's choice. When you provide an IntentSender to createChooser, the sender will be notified by the chooser dialog with the ComponentName chosen by the user. It will be supplied in the extra named EXTRA_CHOSEN_COMPONENT int the IntentSender that is notified.
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
That is not possible.
Other "choosing" solutions, like ShareActionProvider, may offer more. I have not examined the Intent handed to onShareTargetSelected() to see if it contains the ComponentName of the chosen target, though the docs suggest that it should.
And, if for some reason it does not, you are welcome to try to fork ShareActionProvider to add the hooks you want.
The reason why createChooser() cannot be handled this way is simply because the "choosing" is being done by a separate process from yours.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
ShareActionProvider goes in the action bar. You cannot just create an instance, call a couple of setters, and expect something to happen.
My Android app defines an activity with an intent filter of android.intent.action.CREATE_SHORTCUT, which lets me show up in the list of shortcuts that the user can add to their home page, when they select "Add Shortcut" from the menu or long-click the home page.
In this activity I have the following code (actually happens in a click event after they pick which shortcut to add):
Intent shortcutIntent = new Intent(this,MyActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.myicon);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppName");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
finish();
The shortcut works as I would expect, until I reboot the device. I'm actually testing on the emulator, not real device. The shortcut is still there after a reboot, so I know I didn't wipe user data or anything like that, but it acts like it no longer has the FLAG_ACTIVITY_NEW_TASK setting when clicked.
Example steps to recreate (assume my app is an email inbox for clarity):
Create the shortcut
Launch my inbox activity from my main menu acitvity, which uses the NEW_TASK flag.
From the inbox activity, click a message to open the view message activity.
Press HOME key
Click the shortcut -- at this point it brings the entire "task stack" back to the front, with the view message activity on top of the stack, clicking back goes back to the inbox activity, as I would expect
Reboot the device
Repeat steps 2 through 5.. now when I click the shortcut, instead of bringing the view message activity to the front, it brings the task to the front, but then adds a new inbox activity on top of the stack. So pressing BACK once goes back to the view message activity, and back again to the inbox activity.
I also tried setting different properties such as singleTask for my inbox activity in the app manifest, but haven't had any luck. Is this a known issue that flags are not saved with shortcuts?
I think I'll try adding a new stub activity that does nothing but launches the real activity with the NEW_TASK flag and then exits, and have my shortcuts point to that instead. However, seems like a lot of overhead, so hopefully someone has a better answer.
In my application I need to disable display power off when device is charging. There is an option in Developer Menu to disable it, so I can to send Intent for user to enable it.
Also I've found info about PowerManager and WakeLocks, but it is for Alarms, I think. And I must to handle, is device charging.
What is the better, or is there another way to do this?
I've do this by that code:
final boolean isStayAwake = isStayAwakeEnabled(context);
if (!isStayAwake) {
intent = new Intent(ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
}
context.startActivity(intent);
There user must check 'stay awake' option
I've used my own ACTION_APPLICATION_DEVELOPMENT_SETTINGS constant because of problems with default one, which have not "com." prefix
Perhaps you want to try FLAG_KEEP_SCREEN_ON from the WindowManager.LayoutParams
I'm trying to make a button in my App open the built in gallery.
public void onClick(View v) {
Intent intentBrowseFiles = new Intent(Intent.ACTION_VIEW);
intentBrowseFiles.setType("image/*");
intentBrowseFiles.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentBrowseFiles);
}
This results in an error message "The application Camera (process com.android.gallery) has stopped unexpectedly."
If I set the Intent action to ACTION_GET_CONTENT it manages to open the gallery but then simply returns the image to my app when a picture is selected which is not what I want.
I'm trying to make a button in my App open the built in browser.
Your question subject says "Gallery". Your first sentence in the question says "browser". These are not the same thing.
If I set the Intent action to ACTION_GET_CONTENT it manages to open the gallery but then simply returns the image to my app when a picture is selected which is not what I want.
Of course, actually telling us "what [you] want" would just be too useful, so you are making us guess.
I am going to go out on a limb and guess that you are trying to open the Gallery application just as a normal application. Note that there is no Gallery application in the Android OS. There may or may not be a Gallery application on any given device, and it may or may not be one from the Android open source project.
However, for devices that have the Android Market on them, they should support an ACTION_VIEW Intent with a MIME type obtained from android.provider.MediaStore.Images.Media.CONTENT_TYPE.