/**
* 获取手机串号(IMEI)
*
* @param context
* @return
*/
public static String getIMEI(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
if (imei == null) {
return "";
} else {
return imei;
}
}
/**
* 获取用户识别码(IMSI)
*
* @param context
* @return
*/
public static String getSubscriberId(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String tel = tm.getSubscriberId();
return TextUtils.isEmpty(tel) ? "" : tel;
}
/**
* 获取手机号码
*
* @param context
* @return
*/
public static String getPhoneNumber(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getLine1Number();
}
/**
* 获取手机型号
*
* @return
*/
public static String getPhoneModel() {
return Build.MODEL;
}
/**
* 获取运营商<br>
* 其中46000、46002和46007标识中国移动,46001标识中国联通,46003标识中国电信
*
* @param context
* @return
*/
public static String getMNC(Context context) {
String providersName = "";
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY) {
providersName = telephonyManager.getSimOperator();
providersName = providersName == null ? "" : providersName;
}
return providersName;
}
/**
* 获取系统版本,如1.5,2.1
*
* @return SDK版本号
*/
public static String getSysVersionName() {
return Build.VERSION.RELEASE;
}
/**
* 获取SDK版本号
*
* @return
*/
public static int getSdkInt() {
return Build.VERSION.SDK_INT;
}
/**
* 获取包名
*
* @return
*/
public static String getPackageName(Context context) {
final String packageName = context.getPackageName();
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
packageName, 0);
return info.packageName;
} catch (NameNotFoundException e) {
return "";
}
}
/**
* 获取版本名称
*
* @return
*/
public static String getVersionName(Context context) {
final String packageName = context.getPackageName();
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
packageName, 0);
return info.versionName;
} catch (NameNotFoundException e) {
return "";
}
}
/**
* 获取版本号
*
* @return
*/
public static int getVersionCode(Context context) {
final String packageName = context.getPackageName();
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
packageName, 0);
return info.versionCode;
} catch (NameNotFoundException e) {
return 0;
}
}
/**
* 获取安装包信息
*
* @param context
* @param filePath
* @return
*/
public static PackageInfo getPackageInfo(Context context, String filePath) {
if (!TextUtils.isEmpty(filePath)) {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageArchiveInfo(filePath, 0);
return pi;
} else {
return null;
}
}
/**
* 获取应用名称
*
* @param context
* @param packageName
* @return
*/
public static String getAppName(Context context, String packageName) {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = context.getPackageManager();
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (NameNotFoundException e) {
applicationInfo = null;
}
String applicationName = (String) packageManager
.getApplicationLabel(applicationInfo);
return applicationName;
}
public static String getLanguage(Context c) {
if (c != null)
return c.getResources().getConfiguration().locale.getLanguage();
return null;
}
/**
* 获取Mac 地址
*
* @return
*/
public String getMacAddress(Context c) {
WifiManager wifi = (WifiManager) c
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String mac = info.getMacAddress();
if (mac == null) {
return "";
}
mac = mac.replaceAll(":", "");
return mac.toLowerCase();
/**
* 获得屏幕高度
*
* @param context
* @return
*/
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* 获得屏幕宽度
*
* @param context
* @return
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/**
* 获得状态栏的高度
*
* @param context
* @return
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
/**
* 获取当前屏幕截图,包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 获取当前屏幕截图,不包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bp;
}
/**
* 创建快捷方式
*
* @param context
* @param shortCutName
* @param iconId
* @param presentIntent
*/
public static void createShortcut(Context context, String shortCutName,
int iconId, Intent presentIntent) {
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra("duplicate", false);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCutName);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, iconId));
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, presentIntent);
context.sendBroadcast(shortcutIntent);
}
/**
* 读取RAW文件内容
*
* @param context
* @param resid
* @param encoding
* @return
*/
public static String getRawFileContent(Context context, int resid,
String encoding) {
InputStream is = context.getResources().openRawResource(resid);
if (is != null) {
ByteArrayBuffer bab = new ByteArrayBuffer(1024);
int read;
try {
while ((read = is.read()) != -1) {
bab.append(read);
}
return EncodingUtils.getString(bab.toByteArray(), encoding);
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
}
}
}
return "";
}
Android系统工具类笔记
最新推荐文章于 2023-06-29 14:16:02 发布