手机配置
public class CommonUtils {
static Context context;
// 手机网络类型
public static final int NETTYPE_WIFI = 0x01;
public static final int NETTYPE_CMWAP = 0x02;
public static final int NETTYPE_CMNET = 0x03;
public static boolean GTE_HC;
public static boolean GTE_ICS;
public static boolean PRE_HC;
private static Boolean _hasBigScreen = null;
private static Boolean _hasCamera = null;
private static Boolean _isTablet = null;
private static Integer _loadFactor = null;
private static int _pageSize = -1;
public static float displayDensity = 0.0F;
public static final String TAG = context.getClass().getSimpleName();
static {
GTE_ICS = Build.VERSION.SDK_INT >= 14;
GTE_HC = Build.VERSION.SDK_INT >= 11;
PRE_HC = Build.VERSION.SDK_INT >= 11 ? false : true;
}
public CommonUtils() {
throw new AssertionError();
}
/**
* 获取系统当前当前时间戳
*/
public static String getTimesTamp() {
long timestamp = System.currentTimeMillis() / 1000;
return String.valueOf(timestamp);
}
public static int[] getRealScreenSize(Activity activity) {
int[] size = new int[2];
int screenWidth = 0, screenHeight = 0;
WindowManager w = activity.getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try {
screenWidth = (Integer) Display.class.getMethod("getRawWidth")
.invoke(d);
screenHeight = (Integer) Display.class
.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17)
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d,
realSize);
screenWidth = realSize.x;
screenHeight = realSize.y;
} catch (Exception ignored) {
}
size[0] = screenWidth;
size[1] = screenHeight;
return size;
}
/**
* 是否存在相机
*
* @return
*/
public static final boolean hasCamera(Context context) {
if (_hasCamera == null) {
PackageManager pckMgr = context.getPackageManager();
boolean flag = pckMgr
.hasSystemFeature("android.hardware.camera.front");
boolean flag1 = pckMgr.hasSystemFeature("android.hardware.camera");
boolean flag2;
if (flag || flag1)
flag2 = true;
else
flag2 = false;
_hasCamera = Boolean.valueOf(flag2);
}
return _hasCamera.booleanValue();
}
/**
* 判断是否有物理的menu键
*
* @param context
* @return
*/
public static boolean hasHardwareMenuKey(Context context) {
boolean flag = false;
if (PRE_HC)
flag = true;
else if (GTE_ICS) {
flag = ViewConfiguration.get(context).hasPermanentMenuKey();
} else
flag = false;
return flag;
}
/**
* 是否有google商店
*
* @param activity
* @param pck
* @return
*/
public static boolean gotoGoogleMarket(Activity activity, String pck) {
try {
Intent intent = new Intent();
intent.setPackage("com.android.vending");
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + pck));
activity.startActivity(intent);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 隐藏动画视图
*
* @param view
*/
public static void hideAnimatedView(View view) {
if (PRE_HC && view != null)
view.setPadding(view.getWidth(), 0, 0, 0);
}
/**
* 显示动画视图
*
* @param view
*/
public static void showAnimatedView(View view) {
if (PRE_HC && view != null)
view.setPadding(0, 0, 0, 0);
}
/**
* 显示键盘dialog
*
* @param dialog
*/
public static void showSoftKeyboard(Dialog dialog) {
dialog.getWindow().setSoftInputMode(4);
}
/**
* 切换键盘
*
* @param view
*/
public static void toogleSoftKeyboard(Context context, View view) {
((InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE))
.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 判断是否横屏
*
* @return
*/
public static boolean isLandscape(Context context) {
boolean flag;
if (context.getResources().getConfiguration().orientation == 2)
flag = true;
else
flag = false;
return flag;
}
/**
* 判断是否竖屏
*
* @return
*/
public static boolean isPortrait(Context context) {
boolean flag = true;
if (context.getResources().getConfiguration().orientation != 1)
flag = false;
return flag;
}
/**
* 判断是否平板
*
* @return
*/
public static boolean isTablet(Context context) {
if (_isTablet == null) {
boolean flag;
if ((0xf & context.getResources().getConfiguration().screenLayout) >= 3)
flag = true;
else
flag = false;
_isTablet = Boolean.valueOf(flag);
}
return _isTablet.booleanValue();
}
/**
* 判断是否有sd卡
*
* @return
*/
public static boolean isSdcardReady() {
return Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState());
}
/**
* 判断系统语言国家
*
* @return
*/
public static String getCurCountryLan(Context context) {
return context.getResources().getConfiguration().locale.getLanguage()
+ "-"
+ context.getResources().getConfiguration().locale.getCountry();
}
/**
* 判断是否中文简体(CN)国家中国
*
* @return
*/
public static boolean isZhCN(Context context) {
String lang = context.getResources().getConfiguration().locale
.getCountry();
if (lang.equalsIgnoreCase("CN")) {
return true;
}
return false;
}
/**
* 获取两个数的百分比
*
* @param p1
* @param p2
* @return
*/
public static String percent(double p1, double p2) {
String str;
double p3 = p1 / p2;
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(5);// 保留的小数位数(精度)
str = nf.format(p3);
return str;
}
public static String percent2(double p1, double p2) {
String str;
double p3 = p1 / p2;
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(0);
str = nf.format(p3);
return str;
}
/**
* 打开本app在应用商店的页面
*
* @param context
*/
public static void openAppInMarket(Context context) {
if (context != null) {
String pckName = context.getPackageName();
try {
String str = "market://details?id=" + pckName;
Intent localIntent = new Intent("android.intent.action.VIEW");
localIntent.setData(Uri.parse(str));
context.startActivity(localIntent);
} catch (Exception ex) {
}
}
}
/**
* 全屏显示,去掉顶部状态栏
*
* @param activity
*/
public static void setFullScreen(Activity activity) {
WindowManager.LayoutParams params = activity.getWindow()
.getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
activity.getWindow().setAttributes(params);
activity.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
/**
* 关闭全屏显示
*
* @param activity
*/
public static void cancelFullScreen(Activity activity) {
WindowManager.LayoutParams params = activity.getWindow()
.getAttributes();
params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().setAttributes(params);
activity.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
/**
* 得到应用包名
*
* @param pckName
* @return
*/
public static PackageInfo getPackageInfo(Context context, String pckName) {
try {
return context.getPackageManager().getPackageInfo(pckName, 0);
} catch (PackageManager.NameNotFoundException e) {
Log.e("", e.getMessage());
}
return null;
}
/**
* 获取版本名称
*
* @return
*/
public static String getVersionName(Context context) {
String name = "";
try {
name = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException ex) {
name = "";
}
return name;
}
/**
* 安装apk
*
* @param context
* @param file
*/
public static void installAPK(Context context, File file) {
if (file == null || !file.exists())
return;
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
/**
* 获得安转的apk
*
* @param file
* @return
*/
public static Intent getInstallApkIntent(File file) {
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
return intent;
}
/**
* 打电话
*
* @param context
* @param number
*/
public static void openDial(Context context, String number) {
Uri uri = Uri.parse("tel:" + number);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
context.startActivity(it);
}
/**
* 发短信
*
* @param context
* @param smsBody
* @param tel
*/
public static void openSMS(Context context, String smsBody, String tel) {
Uri uri = Uri.parse("smsto:" + tel);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", smsBody);
context.startActivity(it);
}
public static void openDail(Context context) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public static void openSendMsg(Context context) {
Uri uri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
/**
* 调用系统相机
*
* @param context
*/
public static void openCamera(Context context) {
Intent intent = new Intent(); // 调用照相机
intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
intent.setFlags(0x34c40000);
context.startActivity(intent);
}
/**
* 获取移动设备标识码 需要权限android.permission.READ_PHONE_STATE
*
* @return
*/
public static String getIMEI(Context context) {
TelephonyManager tel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return tel.getDeviceId();
}
/**
* 获得手机型号
*
* @return
*/
public static String getPhoneType() {
return Build.MODEL;
}
/**
* 打开手机上安装的指定包名的app
*
* @param context
* @param packageName
*/
public static void openApp(Context context, String packageName) {
Intent mainIntent = context.getPackageManager()
.getLaunchIntentForPackage(packageName);
if (mainIntent == null) {
mainIntent = new Intent(packageName);
} else {
Log.i(TAG, "Action:" + mainIntent.getAction());
}
context.startActivity(mainIntent);
}
public static boolean openAppActivity(Context context, String packageName,
String activityName) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, activityName);
intent.setComponent(cn);
try {
context.startActivity(intent);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 发送邮件
*
* @param context
* @param subject
* 主题
* @param content
* 内容
* @param emails
* 邮件地址
*/
public static void sendEmail(Context context, String subject,
String content, String... emails) {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
// 模拟器
// intent.setType("text/plain");
intent.setType("message/rfc822"); // 真机
intent.putExtra(Intent.EXTRA_EMAIL, emails);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
public static boolean hasStatusBar(Activity activity) {
WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
if ((attrs.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
return false;
} else {
return true;
}
}
/**
* 调用系统安装了的应用分享
*
* @param context
* @param title
* @param url
*/
public static void showSystemShareOption(Activity context,
final String title, final String url) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享:" + title);
intent.putExtra(Intent.EXTRA_TEXT, title + " " + url);
context.startActivity(Intent.createChooser(intent, "选择分享"));
}
/**
* 获取当前网络类型
*
* @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
*/
public static int getNetworkType(Context context) {
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if (!TextUtils.isEmpty(extraInfo)) {
if (extraInfo.toLowerCase().equals("cmnet")) {
netType = NETTYPE_CMNET;
} else {
netType = NETTYPE_CMWAP;
}
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = NETTYPE_WIFI;
}
return netType;
}
}