您的位置:首页 > 移动开发 > Android开发

Android工具类之手机组件调用工具类

2016-07-15 15:56 423 查看
/**
* 手机组件调用工具类
*/
public final class PhoneUtil {
private static long lastClickTime;

/**
* Don't let anyone instantiate this class.
*/
private PhoneUtil() {
throw new Error("Do not need instantiate!");
}

/**
* 调用系统发短信界面
*
* @param activity    Activity
* @param phoneNumber 手机号码
* @param smsContent  短信内容
*/
public static void sendMessage(Context activity, String phoneNumber, String smsContent) {
if (phoneNumber == null || phoneNumber.length() < 4) {
return;
}
Uri uri = Uri.parse("smsto:" + phoneNumber);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", smsContent);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(it);
}

/**
* 判断是否为连击
*
* @return boolean
*/
public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < 500) {
return true;
}
lastClickTime = time;
return false;
}

/**
* 获取手机型号
*
* @param context 上下文
* @return String
*/
public static String getMobileModel(Context context) {
try {
String model = android.os.Build.MODEL; // 手机型号
return model;
} catch (Exception e) {
return "未知";
}
}

/**
* 获取手机品牌
*
* @param context 上下文
* @return String
*/
public static String getMobileBrand(Context context) {
try {
String brand = android.os.Build.BRAND; // android系统版本号
return brand;
} catch (Exception e) {
return "未知";
}
}

/**
* 拍照打开照相机!
*
* @param requestcode 返回值
* @param activity    上下文
* @param fileName    生成的图片文件的路径
*/
public static void toTakePhoto(int requestcode, Activity activity, String fileName) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("camerasensortype", 2);// 调用前置摄像头
intent.putExtra("autofocus", true);// 自动对焦
intent.putExtra("fullScreen", false);// 全屏
intent.putExtra("showActionIcons", false);
try {//创建一个当前任务id的文件然后里面存放任务的照片的和路径!这主文件的名字是用uuid到时候在用任务id去查路径!
File file = new File(fileName);
if (!file.exists()) {//如果这个文件不存在就创建一个文件夹!
file.mkdirs();
}
Uri uri = Uri.fromFile(new File(fileName));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(intent, requestcode);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 打开相册
*
* @param requestcode 响应码
* @param activity    上下文
*/
public static void toTakePicture(int requestcode, Activity activity) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
activity.startActivityForResult(intent, requestcode);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: