欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-29 17:33:46
本文实例讲述了android开发实现的intent跳转工具类。分享给大家供大家参考,具体如下: 一、概述 intent的中文意思是“意图,意向”,在android中提供...

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

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