package com.huaye.odyandroidstore.utils; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.content.FileProvider; import android.webkit.MimeTypeMap; import java.io.File; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2016/9/23 * desc : 意图相关工具类 * </pre> */ public class IntentUtils { private IntentUtils() { throw new UnsupportedOperationException("u can't fuck me..."); } /** * 获取安装App(支持6.0)的意图 * * @param filePath 文件路径 * @return intent */ public static Intent getInstallAppIntent(String filePath) { return getInstallAppIntent(FileUtils.getFileByPath(filePath)); } /** * 获取安装App(支持6.0)的意图 * * @param file 文件 * @return intent */ public static Intent getInstallAppIntent(File file) { if (file == null) return null; Intent intent = new Intent(Intent.ACTION_VIEW); String type; if (Build.VERSION.SDK_INT < 23) { type = "application/vnd.android.package-archive"; } else { type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(FileUtils.getFileExtension(file)); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(Utils.getContext(), "com.your.package.fileProvider", file); intent.setDataAndType(contentUri, type); } intent.setDataAndType(Uri.fromFile(file), type); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取卸载App的意图 * * @param packageName 包名 * @return intent */ public static Intent getUninstallAppIntent(String packageName) { Intent intent = new Intent(Intent.ACTION_DELETE); intent.setData(Uri.parse("package:" + packageName)); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取打开App的意图 * * @param packageName 包名 * @return intent */ public static Intent getLaunchAppIntent(String packageName) { return Utils.getContext().getPackageManager().getLaunchIntentForPackage(packageName); } /** * 获取App具体设置的意图 * * @param packageName 包名 * @return intent */ public static Intent getAppDetailsSettingsIntent(String packageName) { Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS"); intent.setData(Uri.parse("package:" + packageName)); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取分享文本的意图 * * @param content 分享文本 * @return intent */ public static Intent getShareTextIntent(String content) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, content); return intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取分享图片的意图 * * @param content 文本 * @param imagePath 图片文件路径 * @return intent */ public static Intent getShareImageIntent(String content, String imagePath) { return getShareImageIntent(content, FileUtils.getFileByPath(imagePath)); } /** * 获取分享图片的意图 * * @param content 文本 * @param image 图片文件 * @return intent */ public static Intent getShareImageIntent(String content, File image) { if (!FileUtils.isFileExists(image)) return null; return getShareImageIntent(content, Uri.fromFile(image)); } /** * 获取分享图片的意图 * * @param content 分享文本 * @param uri 图片uri * @return intent */ public static Intent getShareImageIntent(String content, Uri uri) { Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, content); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType("image/*"); return intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取其他应用组件的意图 * * @param packageName 包名 * @param className 全类名 * @return intent */ public static Intent getComponentIntent(String packageName, String className) { return getComponentIntent(packageName, className, null); } /** * 获取其他应用组件的意图 * * @param packageName 包名 * @param className 全类名 * @param bundle bundle * @return intent */ public static Intent getComponentIntent(String packageName, String className, Bundle bundle) { Intent intent = new Intent(Intent.ACTION_VIEW); if (bundle != null) intent.putExtras(bundle); ComponentName cn = new ComponentName(packageName, className); intent.setComponent(cn); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取关机的意图 * <p>需添加权限 {@code <uses-permission android:name="android.permission.SHUTDOWN"/>}</p> * * @return intent */ public static Intent getShutdownIntent() { Intent intent = new Intent(Intent.ACTION_SHUTDOWN); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取跳至拨号界面意图 * * @param phoneNumber 电话号码 */ public static Intent getDialIntent(String phoneNumber) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取拨打电话意图 * <p>需添加权限 {@code <uses-permission android:name="android.permission.CALL_PHONE"/>}</p> * * @param phoneNumber 电话号码 */ public static Intent getCallIntent(String phoneNumber) { Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phoneNumber)); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取跳至发送短信界面的意图 * * @param phoneNumber 接收号码 * @param content 短信内容 */ public static Intent getSendSmsIntent(String phoneNumber, String content) { Uri uri = Uri.parse("smsto:" + phoneNumber); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", content); return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } /** * 获取拍照的意图 * * @param outUri 输出的uri * @return 拍照的意图 */ public static Intent getCaptureIntent(Uri outUri) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri); return intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK); } /* *//** * 获取选择照片的Intent * * @return *//* public static Intent getPickIntentWithGallery() { Intent intent = new Intent(Intent.ACTION_PICK); return intent.setType("image*//*"); } *//** * 获取从文件中选择照片的Intent * * @return *//* public static Intent getPickIntentWithDocuments() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); return intent.setType("image*//*"); } public static Intent buildImageGetIntent(Uri saveTo, int outputX, int outputY, boolean returnData) { return buildImageGetIntent(saveTo, 1, 1, outputX, outputY, returnData); } public static Intent buildImageGetIntent(Uri saveTo, int aspectX, int aspectY, int outputX, int outputY, boolean returnData) { Intent intent = new Intent(); if (Build.VERSION.SDK_INT < 19) { intent.setAction(Intent.ACTION_GET_CONTENT); } else { intent.setAction(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); } intent.setType("image*//*"); intent.putExtra("output", saveTo); intent.putExtra("aspectX", aspectX); intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra("return-data", returnData); intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); return intent; } public static Intent buildImageCropIntent(Uri uriFrom, Uri uriTo, int outputX, int outputY, boolean returnData) { return buildImageCropIntent(uriFrom, uriTo, 1, 1, outputX, outputY, returnData); } public static Intent buildImageCropIntent(Uri uriFrom, Uri uriTo, int aspectX, int aspectY, int outputX, int outputY, boolean returnData) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uriFrom, "image*//*"); intent.putExtra("crop", "true"); intent.putExtra("output", uriTo); intent.putExtra("aspectX", aspectX); intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra("return-data", returnData); intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); return intent; } public static Intent buildImageCaptureIntent(Uri uri) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); return intent; }*/ }