您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何在Android中編寫一個Intent跳轉工具類,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
一、概述
Intent的中文意思是“意圖,意向”,在Android中提供了Intent機制來協助應用間的交互與通訊,Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,并完成組件的調用。Intent不僅可用于應用程序之間,也可用于應用程序內部的Activity/Service之間的交互。因此,可以將Intent理解為不同組件之間通信的“媒介”專門提供組件互相調用的相關信息。
Intent可以啟動一個Activity,也可以啟動一個Service,還可以發起一個廣播Broadcasts。
二、Intent跳轉工具類代碼
/** * 進行頁面跳轉的工具 * * @author chen.lin * */ public class IntentUtil { private static final String IMAGE_TYPE = "image/*"; private static final String TAG = "IntentUtil"; /** * 進行頁面跳轉 * * @param clzz */ public static void showIntent(Activity context, Class<?> clzz, String[] keys, String[] values) { Intent intent = new Intent(context, clzz); if (values != null && values.length > 0) { for (int i = 0; i < values.length; i++) { if (!TextUtils.isEmpty(keys[i]) && !TextUtils.isEmpty(values[i])) { intent.putExtra(keys[i], values[i]); } } } context.startActivity(intent); } public static void showIntent(Activity context, Class<?> clzz) { showIntent(context, clzz, null, null); } /** * 打電話 * * @param intent * @param context * @param tel */ public static void openCall(Context context, String tel) { tel = tel.replaceAll("-", ""); Intent intent = new Intent(); // 激活源代碼,添加intent對象 intent.setAction("android.intent.action.CALL"); intent.setData(Uri.parse("tel:" + tel)); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(intent); } /*** * 從相冊中取圖片 */ public static void pickPhoto(Activity context, int requestCode) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); context.startActivityForResult(intent, requestCode); } /** * 拍照獲取圖片 */ public static void takePhoto(Activity context, int requestCode, Uri cameraUri) { // 執行拍照前,應該先判斷SD卡是否存在 String SDState = Environment.getExternalStorageState(); if (SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE" Logger.i(TAG, "cameraUri.path------>" + cameraUri.getPath()); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, cameraUri); context.startActivityForResult(intent, requestCode); } else { Toast.makeText(context, "內存卡不存在", Toast.LENGTH_LONG).show(); } } /** * 拍照 * * @param context * @param uri */ public static void takePhoto(Activity context, Uri uri, int requestCode) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DISPLAY_NAME, String.valueOf(System.currentTimeMillis()) + ".jpg"); values.put(MediaStore.Images.Media.MIME_TYPE, "image/*"); uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); context.startActivityForResult(intent, requestCode); } else { Toast.makeText(context, "內存卡不存在", Toast.LENGTH_LONG).show(); } } /** * 本地照片調用 * * @param context * @param requestCode */ public void openPhotos(Activity context, int requestCode) { if (openPhotosNormal(context, requestCode) // && openPhotosBrowser(context, requestCode) // && openPhotosFinally(context)) ; } /** * 這個是找不到相關的圖片瀏覽器,或者相冊 */ private boolean openPhotosFinally(Activity context) { Toast.makeText(context, "您的系統沒有文件瀏覽器或則相冊支持,請安裝!", Toast.LENGTH_LONG).show(); return false; } /** * 獲取從本地圖庫返回來的時候的URI解析出來的文件路徑 * * @return */ public static String getPhotoPathByLocalUri(Context context, Intent data) { Uri photoUri = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(photoUri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); return picturePath; } /** * PopupMenu打開本地相冊. */ private boolean openPhotosNormal(Activity activity, int actResultCode) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_TYPE); try { activity.startActivityForResult(intent, actResultCode); } catch (android.content.ActivityNotFoundException e) { return true; } return false; } /** * 打開其他的一文件瀏覽器,如果沒有本地相冊的話 */ private boolean openPhotosBrowser(Activity activity, int requestCode) { Toast.makeText(activity, "沒有相冊軟件,運行文件瀏覽器", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT" intent.setType(IMAGE_TYPE); // 查看類型 String IMAGE_UNSPECIFIED = // "image/*"; Intent wrapperIntent = Intent.createChooser(intent, null); try { activity.startActivityForResult(wrapperIntent, requestCode); } catch (android.content.ActivityNotFoundException e1) { return true; } return false; } /** * 打開照相機 * * @param activity * 當前的activity * @param requestCode * 拍照成功時activity forResult 的時候的requestCode * @param photoFile * 拍照完畢時,圖片保存的位置 */ @SuppressLint("SimpleDateFormat") public static Uri openCamera(Activity context, int requestCode) { // 執行拍照前,應該先判斷SD卡是否存在 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); String filename = timeStampFormat.format(new Date()); ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, filename); Uri photoUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); context.startActivityForResult(intent, requestCode); return photoUri; } else { Toast.makeText(context, "內存卡不存在", Toast.LENGTH_LONG).show(); } return null; } /** * 選擇圖片后,獲取圖片的路徑 * * @param requestCode * @param data */ public static void doPhoto(Activity context, Intent data, int requestCode, int value, EditText editText, ImageView imageView, Uri photoUri) { // 從相冊取圖片,有些手機有異常情況,請注意 if (requestCode == value) { if (data == null) { Toast.makeText(context, "選擇圖片文件出錯", Toast.LENGTH_LONG).show(); return; } photoUri = data.getData(); if (photoUri == null) { Toast.makeText(context, "選擇圖片文件出錯", Toast.LENGTH_LONG).show(); return; } } String[] pojo = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME }; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(photoUri, pojo, null, null, null); String picPath = null; String filename = null; if (cursor != null) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); filename = cursor.getString(cursor.getColumnIndexOrThrow(pojo[1])); editText.requestFocus(); editText.setText(filename); cursor.close(); } String dix = filename.substring(filename.lastIndexOf("."), filename.length()); if (filename != null && (dix.equalsIgnoreCase(".png") || dix.equalsIgnoreCase(".jpg") || dix.equalsIgnoreCase(".gif") || dix.equalsIgnoreCase(".bmp") || dix.equalsIgnoreCase(".jpeg") || dix .equalsIgnoreCase(".tiff"))) { // lastIntent.putExtra(KEY_PHOTO_PATH, picPath); imageView.setVisibility(View.VISIBLE); imageView.setImageURI(Uri.parse(picPath)); } else { imageView.setVisibility(View.GONE); Toast.makeText(context, "選擇圖片文件不正確", Toast.LENGTH_LONG).show(); } } /** * FLAG_ACTIVITY_SINGLE_TOP * //當于加載模式中的singletop,在當前中的activity中轉到當前activity,不增加新的 * * @param file */ public static void openFile(Context context, File file) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 默認的跳轉類型,它會重新創建一個新的Activity intent.setAction(android.content.Intent.ACTION_VIEW); // 調用getMIMEType()來取得MimeType String type = FileUtil.getMIMEType(file); // 設置intent的file與MimeType intent.setDataAndType(Uri.fromFile(file), type); context.startActivity(intent); } /** * 截取圖片 * * @param uri * @param outputX * @param outputY * @param requestCode */ public static void cropImage(Activity context, Uri uri, int outputX, int outputY, int requestCode) { // 裁剪圖片意圖 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); // 裁剪框的比例,1:1 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // 裁剪后輸出圖片的尺寸大小 intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); // 圖片格式 intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", true); context.startActivityForResult(intent, requestCode); } /** * 獲取圖片的旋轉角度 * * @param path * @return */ public static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(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) { e.printStackTrace(); } return degree; } /** * 保存文件 * * @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 = data.getData(); cropImage(context, photoUri, 500, 500, requestCode); if (photoUri != null) { photo = BitmapFactory.decodeFile(photoUri.getPath()); } if (photo == null) { Bundle extra = data.getExtras(); if (extra != null) { photo = (Bitmap) extra.get("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, stream); } } imageView.setImageBitmap(photo); } /** * 保存照相后的圖片 * * @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 = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); imageView.setImageBitmap(photo); } if (cameraUri != null) { String path = cameraUri.getPath(); Logger.i(TAG, "path-->" + path); String filename = path.substring(path.lastIndexOf("/") + 1, path.length()); editText.setText(filename); } } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
關于如何在Android中編寫一個Intent跳轉工具類就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。