Last active
September 11, 2018 19:32
-
-
Save jandrop/5fdb1caacf474dcf16f2527f4b5a68e8 to your computer and use it in GitHub Desktop.
Collection of Intents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.atrapandocucarachas.utils; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.provider.Settings; | |
import android.text.TextUtils; | |
import android.widget.Toast; | |
/** | |
* @author Alejandro Platas Mallo | |
* @version 1.00 | |
* @since 3/8/15 | |
*/ | |
public class IntentUtils { | |
public IntentUtils() { | |
//Empty constructor | |
} | |
public static void sendEmail(Activity activity, String to, String subject, String text) { | |
sendEmail(activity, new String[]{to}, subject, text); | |
} | |
/** | |
* Create intent to send email by Gmail app | |
* | |
* @param activity Activity | |
* @param to Destination | |
* @param subject Subject | |
* @param text Text | |
*/ | |
public static void sendEmail(Activity activity, String[] to, String subject, String text) { | |
Intent intent = new Intent(Intent.ACTION_SEND); | |
intent.setType("message/rfc822"); | |
intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail"); | |
intent.putExtra(Intent.EXTRA_EMAIL, to); | |
if (subject != null) | |
intent.putExtra(Intent.EXTRA_SUBJECT, subject); | |
if (text != null) | |
intent.putExtra(Intent.EXTRA_TEXT, text); | |
activity.startActivity(intent); | |
} | |
/** | |
* Intent to make a phone call | |
* | |
* @param activity Activity | |
* @param phoneNumber Phone to call | |
*/ | |
public static void phoneCall(Activity activity, String phoneNumber) { | |
Intent call = new Intent(); | |
call.setAction(Intent.ACTION_CALL); | |
call.setData(Uri.parse("tel:" + phoneNumber)); | |
activity.startActivity(call); | |
} | |
/** | |
* Intent to show StreetView from coordinates | |
* | |
* @param activity Activity | |
* @param latitude Latitude | |
* @param longitude Longitude | |
*/ | |
public static void showStreetView(Activity activity, double latitude, double longitude) { | |
StringBuilder builder = new StringBuilder("google.streetview:cbll="); | |
builder.append(latitude).append(",").append(longitude); | |
Intent intent = new Intent(); | |
intent.setAction(Intent.ACTION_VIEW); | |
intent.setData(Uri.parse(builder.toString())); | |
activity.startActivity(intent); | |
} | |
/** | |
* Intent to open GoogleMaps on coordinates | |
* | |
* @param activity Activity | |
* @param latitude Latitude | |
* @param longitude Longitude | |
* @param zoomLevel ZoomLevel | |
*/ | |
public static void openMaps(Activity activity, double latitude, double longitude, Integer zoomLevel) { | |
Intent intent = new Intent(); | |
intent.setAction(Intent.ACTION_VIEW); | |
String data = String.format("geo:%s,%s", latitude, longitude); | |
if (zoomLevel != null) { | |
data = String.format("%s?z=%s", data, zoomLevel); | |
} | |
intent.setData(Uri.parse(data)); | |
activity.startActivity(intent); | |
} | |
/** | |
* Intent to Open Location Services | |
* | |
* @param activity Activity | |
*/ | |
public static void showLocationServices(Activity activity) { | |
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); | |
activity.startActivity(intent); | |
} | |
/** | |
* Intent to Open a web link | |
* | |
* @param activity Activity | |
* @param url Url to open | |
*/ | |
public static void openLink(Activity activity, String url) { | |
// if protocol isn't defined use http by default | |
if (!TextUtils.isEmpty(url) && !url.contains("://")) { | |
url = "http://" + url; | |
} | |
Intent intent = new Intent(); | |
intent.setAction(Intent.ACTION_VIEW); | |
intent.setData(Uri.parse(url)); | |
activity.startActivity(intent); | |
} | |
public static void sendWhatsApp(Context context, String text) { | |
PackageManager pm = context.getPackageManager(); | |
try { | |
Intent intent = new Intent(Intent.ACTION_SEND); | |
intent.setType("text/plain"); | |
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA); | |
intent.setPackage("com.whatsapp"); | |
intent.putExtra(Intent.EXTRA_TEXT, text); | |
context.startActivity(Intent.createChooser(intent, "Share with")); | |
} catch (PackageManager.NameNotFoundException e) { | |
Toast.makeText(context, "WhatsApp not Installed", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public static void shareApplication(Context context) { | |
String packageName = context.getPackageName(); | |
String url = "https://play.google.com/store/apps/details?id=" + packageName + "&hl=es"; | |
String shareText = "Hey! Échale un vistazo a: \"" + url; | |
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); | |
shareIntent.setType("text/plain"); | |
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareText); | |
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "He compartido una aplicación contigo..."); | |
context.startActivity(Intent.createChooser(shareIntent, "Compartir vía...")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment