SoFunction
Updated on 2025-04-09

An example of the Intent jump tool class implemented by Android development

This article describes the Intent jump tool class implemented by Android development. Share it for your reference, as follows:

1. Overview

Intent means "intention, intention" in Chinese. It provides an Intent mechanism in Android to assist in the interaction and communication between applications. Intent is responsible for describing the actions, data involved in the action and additional data of an operation in the application. Android is responsible for finding the corresponding components, passing the Intent to the calling components, and completing the component call. Intent can be used not only between applications, but also between activities/Services within applications. Therefore, Intent can be understood as a "mediate" of communication between different components specifically providing relevant information about components calling each other.

Intent can start an activity, a service, or a broadcast Broadcasts.

2. Intent jump tool code

/**
  * Tools for page redirection
  *
  * @author
  *
  */
public class IntentUtil {
  private static final String IMAGE_TYPE = "image/*";
   private static final String TAG = "IntentUtil";
   /**
    * Make page jump
    *
    * @param clzz
    */
  public static void showIntent(Activity context, Class<?> clzz, String[] keys, String[] values) {
    Intent intent = new Intent(context, clzz);
    if (values != null &&  > 0) {
      for (int i = 0; i < ; i++) {
        if (!(keys[i]) && !(values[i])) {
          (keys[i], values[i]);
        }
      }
    }
    (intent);
  }
  public static void showIntent(Activity context, Class<?> clzz) {
    showIntent(context, clzz, null, null);
  }
  /**
    * Call up
    *
    * @param intent
    * @param context
    * @param tel
    */
  public static void openCall(Context context, String tel) {
    tel = ("-", "");
    Intent intent = new Intent();
    // Activate the source code and add an intent object    ("");
    (("tel:" + tel));
    (Intent.FLAG_ACTIVITY_CLEAR_TOP);
    (intent);
  }
  /***
    * Get pictures from the album
    */
  public static void pickPhoto(Activity context, int requestCode) {
    Intent intent = new Intent();
    ("image/*");
     (Intent.ACTION_GET_CONTENT);
     (intent, requestCode);
   }
   /**
    * Take a photo to get pictures
    */
  public static void takePhoto(Activity context, int requestCode, Uri cameraUri) {
    // Before performing the photo, you should first determine whether the SD card exists    String SDState = ();
    if ((Environment.MEDIA_MOUNTED)) {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// ".IMAGE_CAPTURE"
      (TAG, "------>" + ());
      (.EXTRA_OUTPUT, cameraUri);
      (intent, requestCode);
    } else {
      (context, "The memory card does not exist", Toast.LENGTH_LONG).show();
    }
  }
  /**
    * Photograph
    *
    * @param context
    * @param uri
    */
  public static void takePhoto(Activity context, Uri uri, int requestCode) {
    if (().equals(Environment.MEDIA_MOUNTED)) {
      Intent intent = new Intent(".IMAGE_CAPTURE");
      ContentValues values = new ContentValues();
      (.DISPLAY_NAME, (()) + ".jpg");
      (.MIME_TYPE, "image/*");
       uri = ().insert(.EXTERNAL_CONTENT_URI, values);
       // (, 0);
       (MediaStore.EXTRA_OUTPUT, uri);
       (intent, requestCode);
     } else {
       (context, "The memory card does not exist", Toast.LENGTH_LONG).show();
     }
   }
   /**
    * Local photo call
    *
    * @param context
    * @param requestCode
    */
  public void openPhotos(Activity context, int requestCode) {
    if (openPhotosNormal(context, requestCode) //
        && openPhotosBrowser(context, requestCode) //
        && openPhotosFinally(context))
      ;
  }
  /**
    * This is the relevant image browser or photo album that cannot be found
    */
  private boolean openPhotosFinally(Activity context) {
    (context, "Your system does not have file browser or photo album support, please install it!", Toast.LENGTH_LONG).show();
    return false;
  }
  /**
    * Get the file path parsed from the URI when returning from the local library
    *
    * @return
    */
  public static String getPhotoPathByLocalUri(Context context, Intent data) {
    Uri photoUri = ();
    String[] filePathColumn = {  };
    Cursor cursor = ().query(photoUri, filePathColumn, null, null, null);
    ();
    int columnIndex = (filePathColumn[0]);
    String picturePath = (columnIndex);
    ();
    return picturePath;
  }
  /**
    * PopupMenu opens local album.
    */
  private boolean openPhotosNormal(Activity activity, int actResultCode) {
    Intent intent = new Intent(Intent.ACTION_PICK, null);
    (.EXTERNAL_CONTENT_URI, IMAGE_TYPE);
    try {
      (intent, actResultCode);
    } catch ( e) {
      return true;
    }
    return false;
  }
  /**
    * Open another file browser, if there is no local album
    */
  private boolean openPhotosBrowser(Activity activity, int requestCode) {
    (activity, "No album software, run file browser", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // ".GET_CONTENT"
    (IMAGE_TYPE); // View type String IMAGE_UNSPECIFIED =                  // "image/*";
     Intent wrapperIntent = (intent, null);
     try {
       (wrapperIntent, requestCode);
     } catch (e1) {
       return true;
     }
     return false;
   }
   /**
    * Turn on the camera
    *
    * @param activity
    * Current activity
    * @param requestCode
    * requestCode when the activity forResult is successful
    * @param photoFile
    * When the photo is taken, the location where the picture is saved
    */
  @SuppressLint("SimpleDateFormat")
  public static Uri openCamera(Activity context, int requestCode) {
    // Before performing the photo, you should first determine whether the SD card exists    if (().equals(Environment.MEDIA_MOUNTED)) {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
      String filename = (new Date());
      ContentValues values = new ContentValues();
      (, filename);
      Uri photoUri = ().insert(.EXTERNAL_CONTENT_URI, values);
      (MediaStore.EXTRA_OUTPUT, photoUri);
      (intent, requestCode);
      return photoUri;
    } else {
      (context, "The memory card does not exist", Toast.LENGTH_LONG).show();
    }
    return null;
  }
  /**
    * After selecting the image, get the path to the image
    *
    * @param requestCode
    * @param data
    */
  public static void doPhoto(Activity context, Intent data, int requestCode, int value, EditText editText,
      ImageView imageView, Uri photoUri) {
    // Get pictures from the album. Some phones have abnormalities. Please pay attention.    if (requestCode == value) {
      if (data == null) {
        (context, "Error selecting image file", Toast.LENGTH_LONG).show();
        return;
      }
      photoUri = ();
      if (photoUri == null) {
        (context, "Error selecting image file", Toast.LENGTH_LONG).show();
        return;
      }
    }
    String[] pojo = { , .DISPLAY_NAME };
    ContentResolver resolver = ();
    Cursor cursor = (photoUri, pojo, null, null, null);
    String picPath = null;
    String filename = null;
    if (cursor != null) {
      int columnIndex = (pojo[0]);
      ();
      picPath = (columnIndex);
      filename = ((pojo[1]));
      ();
      (filename);
      ();
    }
    String dix = (("."), ());
    if (filename != null
        && ((".png") || (".jpg") || (".gif")
            || (".bmp") || (".jpeg") || dix
              .equalsIgnoreCase(".tiff"))) {
      // (KEY_PHOTO_PATH, picPath);
      ();
      ((picPath));
    } else {
      ();
      (context, "The picture file is not selected correctly", Toast.LENGTH_LONG).show();
    }
  }
  /**
    * FLAG_ACTIVITY_SINGLE_TOP
    * // When the singletop in the loading mode, it will be transferred to the current activity in the current activity, and no new one will be added.
    *
    * @param file
    */
  public static void openFile(Context context, File file) {
    Intent intent = new Intent();
    (Intent.FLAG_ACTIVITY_NEW_TASK);// The default jump type, it will recreate a new activity    (.ACTION_VIEW);
    // Call getMIMEType() to get MimeType    String type = (file);
    // Set the file and MimeType of the intent    ((file), type);
    (intent);
  }
  /**
    * Snap the picture
    *
    * @param uri
    * @param outputX
    * @param outputY
    * @param requestCode
    */
  public static void cropImage(Activity context, Uri uri, int outputX, int outputY, int requestCode) {
    // Crop the picture intention    Intent intent = new Intent("");
    (uri, "image/*");
     ("crop", "true");
     // The ratio of the cropping box, 1:1
     ("aspectX", 1);
     ("aspectY", 1);
     // The size of the output image after cropping
     ("outputX", outputX);
     ("outputY", outputY);
     // Picture format
     ("outputFormat", "JPEG");
     ("noFaceDetection", true);
     ("return-data", true);
     (intent, requestCode);
   }
   /**
    * Get the rotation angle of the picture
    *
    * @param path
    * @return
    */
  public static int readPictureDegree(String path) {
    int degree = 0;
    try {
      ExifInterface exifInterface = new ExifInterface(path);
      int orientation = (ExifInterface.TAG_ORIENTATION,
          ExifInterface.ORIENTATION_NORMAL);
      switch (orientation) {
      case ExifInterface.ORIENTATION_ROTATE_90:
        degree = 90;
        break;
      case ExifInterface.ORIENTATION_ROTATE_180:
        degree = 180;
        break;
      case ExifInterface.ORIENTATION_ROTATE_270:
        degree = 270;
        break;
      }
    } catch (IOException e) {
      ();
    }
    return degree;
  }
  /**
    * Save the file
    *
    * @param context
    * @param data
    * @param requestCode
    * @param imageView
    */
  public static void saveImage(Activity context, Intent data, int requestCode, ImageView imageView) {
    Bitmap photo = null;
    Uri photoUri = ();
    cropImage(context, photoUri, 500, 500, requestCode);
    if (photoUri != null) {
      photo = (());
    }
    if (photo == null) {
      Bundle extra = ();
      if (extra != null) {
        photo = (Bitmap) ("data");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        (, 100, stream);
      }
    }
    (photo);
  }
  /**
    * Save the pictures after taking pictures
    *
    * @param context
    * @param requestCode
    * @param spath
    * @return
    */
  public static boolean saveCamera(Activity context, Intent data, Uri cameraUri, EditText editText,
      ImageView imageView) {
    try {
      final Bundle extras = ();
      if (extras != null) {
        Bitmap photo = ("data");
        (photo);
      }
      if (cameraUri != null) {
        String path = ();
        (TAG, "path-->" + path);
        String filename = (("/") + 1, ());
        (filename);
      }
    } catch (Exception e) {
      ();
      return false;
    }
    return true;
  }
}

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.