/**
* 手機元件呼叫工具類
*/
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;
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 {
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);
}
}