Android開發(fā)實(shí)現(xiàn)的Intent跳轉(zhuǎn)工具類實(shí)例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)的Intent跳轉(zhuǎn)工具類。分享給大家供大家參考,具體如下:
一、概述
Intent的中文意思是“意圖,意向”,在Android中提供了Intent機(jī)制來(lái)協(xié)助應(yīng)用間的交互與通訊,Intent負(fù)責(zé)對(duì)應(yīng)用中一次操作的動(dòng)作、動(dòng)作涉及數(shù)據(jù)、附加數(shù)據(jù)進(jìn)行描述,Android則根據(jù)此Intent的描述,負(fù)責(zé)找到對(duì)應(yīng)的組件,將 Intent傳遞給調(diào)用的組件,并完成組件的調(diào)用。Intent不僅可用于應(yīng)用程序之間,也可用于應(yīng)用程序內(nèi)部的Activity/Service之間的交互。因此,可以將Intent理解為不同組件之間通信的“媒介”專門提供組件互相調(diào)用的相關(guān)信息。
Intent可以啟動(dòng)一個(gè)Activity,也可以啟動(dòng)一個(gè)Service,還可以發(fā)起一個(gè)廣播Broadcasts。
二、Intent跳轉(zhuǎn)工具類代碼
/**
* 進(jìn)行頁(yè)面跳轉(zhuǎn)的工具
*
* @author chen.lin
*
*/
public class IntentUtil {
private static final String IMAGE_TYPE = "image/*";
private static final String TAG = "IntentUtil";
/**
* 進(jìn)行頁(yè)面跳轉(zhuǎn)
*
* @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對(duì)象
intent.setAction("android.intent.action.CALL");
intent.setData(Uri.parse("tel:" + tel));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
/***
* 從相冊(cè)中取圖片
*/
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) {
// 執(zhí)行拍照前,應(yīng)該先判斷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, "內(nèi)存卡不存在", 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, "內(nèi)存卡不存在", Toast.LENGTH_LONG).show();
}
}
/**
* 本地照片調(diào)用
*
* @param context
* @param requestCode
*/
public void openPhotos(Activity context, int requestCode) {
if (openPhotosNormal(context, requestCode) //
&& openPhotosBrowser(context, requestCode) //
&& openPhotosFinally(context))
;
}
/**
* 這個(gè)是找不到相關(guān)的圖片瀏覽器,或者相冊(cè)
*/
private boolean openPhotosFinally(Activity context) {
Toast.makeText(context, "您的系統(tǒng)沒有文件瀏覽器或則相冊(cè)支持,請(qǐng)安裝!", Toast.LENGTH_LONG).show();
return false;
}
/**
* 獲取從本地圖庫(kù)返回來(lái)的時(shí)候的URI解析出來(lái)的文件路徑
*
* @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打開本地相冊(cè).
*/
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;
}
/**
* 打開其他的一文件瀏覽器,如果沒有本地相冊(cè)的話
*/
private boolean openPhotosBrowser(Activity activity, int requestCode) {
Toast.makeText(activity, "沒有相冊(cè)軟件,運(yùn)行文件瀏覽器", 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;
}
/**
* 打開照相機(jī)
*
* @param activity
* 當(dāng)前的activity
* @param requestCode
* 拍照成功時(shí)activity forResult 的時(shí)候的requestCode
* @param photoFile
* 拍照完畢時(shí),圖片保存的位置
*/
@SuppressLint("SimpleDateFormat")
public static Uri openCamera(Activity context, int requestCode) {
// 執(zhí)行拍照前,應(yīng)該先判斷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, "內(nèi)存卡不存在", 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) {
// 從相冊(cè)取圖片,有些手機(jī)有異常情況,請(qǐng)注意
if (requestCode == value) {
if (data == null) {
Toast.makeText(context, "選擇圖片文件出錯(cuò)", Toast.LENGTH_LONG).show();
return;
}
photoUri = data.getData();
if (photoUri == null) {
Toast.makeText(context, "選擇圖片文件出錯(cuò)", 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
* //當(dāng)于加載模式中的singletop,在當(dāng)前中的activity中轉(zhuǎn)到當(dāng)前activity,不增加新的
*
* @param file
*/
public static void openFile(Context context, File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 默認(rèn)的跳轉(zhuǎn)類型,它會(huì)重新創(chuàng)建一個(gè)新的Activity
intent.setAction(android.content.Intent.ACTION_VIEW);
// 調(diào)用getMIMEType()來(lái)取得MimeType
String type = FileUtil.getMIMEType(file);
// 設(shè)置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);
}
/**
* 獲取圖片的旋轉(zhuǎn)角度
*
* @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;
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)打開各種文件的intent方法小結(jié)
- Android編程開發(fā)之打開文件的Intent及使用方法
- Android使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android使用Intent獲取聯(lián)系人信息
- Android Intent的幾種用法詳細(xì)解析
- Android Intent啟動(dòng)別的應(yīng)用實(shí)現(xiàn)方法
- 詳解Android中Intent的使用方法
- 詳解Android應(yīng)用開發(fā)中Intent的作用及使用方法
- Android系列之Intent傳遞對(duì)象的幾種實(shí)例方法
- Android開發(fā)中使用Intent打開第三方應(yīng)用及驗(yàn)證可用性的方法詳解
相關(guān)文章
Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)
這篇文章主要介紹了Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android 實(shí)現(xiàn)把bitmap圖片的某一部分的顏色改成其他顏色
這篇文章主要介紹了Android 實(shí)現(xiàn)把bitmap圖片的某一部分的顏色改成其他顏色,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
VS Code開發(fā)React-Native及Flutter 開啟無(wú)線局域網(wǎng)安卓真機(jī)調(diào)試問(wèn)題
這篇文章主要介紹了VS Code開發(fā)React-Native,F(xiàn)lutter 開啟無(wú)線局域網(wǎng)安卓真機(jī)調(diào)試,需要的朋友可以參考下2020-04-04
Android中使用SharedPreferences完成記住賬號(hào)密碼的功能
這篇文章主要介紹了Android中使用SharedPreferences完成記住賬號(hào)密碼的功能,需要的朋友可以參考下2017-08-08
Android實(shí)現(xiàn)自動(dòng)播放圖片功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)播放圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
mui.init()與mui.plusReady()區(qū)別和關(guān)系
給大家分享一下在使用MUI進(jìn)行APP開發(fā)的時(shí)候,mui.init()與mui.plusReady()區(qū)別以及使用上不同之處。2017-11-11

