栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android开发实现的Intent跳转工具类实例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号