这篇文章给大家介绍如何在Android中编写一个Intent跳转工具类,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、概述Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的
这篇文章给大家介绍如何在Android中编写一个Intent跳转工具类,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
一、概述
Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。
Intent可以启动一个Activity,也可以启动一个Service,还可以发起一个广播Broadcasts。
二、Intent跳转工具类代码
public class IntentUtil { private static final String IMAGE_TYPE = "image 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); } 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 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(); } } 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 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; } 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; } 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 @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; } 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(); } } 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); } 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 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; } 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); } 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跳转工具类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
--结束END--
本文标题: 如何在Android中编写一个Intent跳转工具类
本文链接: https://lsjlt.com/news/220146.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0