Skip to content
Stanislav Koshutsky edited this page Feb 15, 2017 · 4 revisions

public class IntentHelper {

public static boolean openUrlInBrowser(final Context context, final String url)

Opens geven url in an external browser like Google Chrome or any other chosen by user in a picker browser if there are more than 1 installed. Returns false in case of SecurityException or no browser app could be found at all.

public static Intent getExplicitIntent(Context context, Intent implicitIntent)  

Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
"java.lang.IllegalArgumentException: Service Intent must be explicit"
If you are using an implicit intent, and know only 1 target would answer this intent,
This method will help you turn the implicit intent into the explicit form.
Inspired by SO answer: http://stackoverflow.com/a/26318757/1446466

public static void shareByEmail(final Context context,
                                final String[] receivers,
                                final String subject,
                                final String text,
                                String pickerTitle,
                                String securityExceptionMessage,
                                String noAssociatedAppErrorMessage)  

Starts EMail intent with filled by given receiver, subject and text and custom picker title and exception messages. It uses corresponding EMailHelper.sendEmail() method.

public static void shareByEmail(final Context context,
                                final String subject,
                                final String text,
                                String pickerTitle,
                                String securityExceptionMessage,
                                String noAssociatedAppErrorMessage)  

Starts EMail intent with filled by given receiver, subject and text, uses EMailHelper.sendEmail() method.

public static void shareByEmail(final Context context,
                                final String subject,
                                final String text,
                                String pickerTitle)  

Starts EMail intent with filled by given receiver, subject and text with custom picker title. It uses corresponding EMailHelper.sendEmail() method.

public static void shareByEmail(final Context context,
                                final String subject,
                                final String text)  

Starts EMail intent with filled by given receiver, subject and text. It uses corresponding EMailHelper.sendEmail() method.

public static boolean shareText(final Context context,
                                final String text)  

Starts sharing intent to share a text. This Intent could be handled by any application like ES Explorer, Social Networking apps, etc.
It creates Intent for Intent.ACTION_SEND action and puts given text as Intent.EXTRA_TEXT with type "text/plain".

public static boolean shareText(final Context context,
                                final String text,
                                final String pickerTitle)  

Starts sharing intent to share a text. This Intent could be handled by any application like ES Explorer, Social Networking apps, etc.
It creates Intent for Intent.ACTION_SEND action and puts given text as Intent.EXTRA_TEXT with type "text/plain". The only difference to previous method is pickerTitle - a custom application picker title.

public static boolean shareImage(final Context context,
                                 final File imageFile)  

Starts sharing intent to share a text. This Intent could be handled by any application which could handle images like Social Networking apps, ES Explorer, etc.
It creates Intent for Intent.ACTION_SEND action and puts given image file as Intent.EXTRA_STREAM with type "image/*".

public static boolean shareImage(final Context context,
                                 final File imageFile,
                                 final String shareMessage,
                                 final String pickerTitle)  

Starts sharing intent to share a text. This Intent could be handled by any application which could handle images with text message like Social Networking apps.
It creates Intent for Intent.ACTION_SEND action and puts given image file as Intent.EXTRA_STREAM with type "image/*". The shareMessage will be added as Intent.EXTRA_TEXT with type "text/plain".
The pickerTitle is a custom application picker title.

public static boolean shareAudio(final Context context,
                                 final File messageFileMp3)  

Starts sharing intent to share a mp3-file. This Intent could be handled by any application which handles mp3 like Social Networking apps.
It creates Intent for Intent.ACTION_SEND action and puts given mp3-file as Intent.EXTRA_STREAM with type "audio/*".

public static boolean shareAudio(final Context context,
                                 final File messageFileMp3,
                                 final String shareMessage,
                                 final String pickerTitle)

Starts sharing intent to share a mp3-file + text message. This Intent could be handled by any application which handles mp3 together with text like Social Networking apps.
It creates Intent for Intent.ACTION_SEND action and puts given mp3-file as Intent.EXTRA_STREAM with type "audio/*". The shareMessage will be added as Intent.EXTRA_TEXT with type "text/plain".
The pickerTitle is a custom application picker title.

public static boolean dialPhoneNumber(final Context context, final String phoneNumber)  

Starts phone dialing Intent with Intent.ACTION_DIAL action and phoneNumber as its data. Method returns false if dialing Intent starting failed due to Exception (expecting only SecurityException, however there might be other exceptions due to phone hardware missing, etc.)

public static boolean showMap(final Context context, final Uri geoLocation)  

Starts Intent which usually corresponds to Google maps application, so it shows map positioned by geoLocation. It is intent with Intent.ACTION_VIEW action and given geoLocation as data.
Method returns false if Intent starting failed due to any Exception (expecting only SecurityException).

Clone this wiki locally