Android实用代码片段(二)
16lz
2022-01-25
1、唤醒屏幕并解锁:
public static void wakeUpAndUnlock(Context context){ KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock"); //解锁 kl.disableKeyguard(); //获取电源管理器对象 PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE); //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright"); //点亮屏幕 wl.acquire(); //释放 wl.release(); }
2、判断当前App处于前台还是后台状态:
public static boolean isApplicationBackground(final Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); @SuppressWarnings("deprecation") List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } 权限:<uses-permission android:name="android.permission.GET_TASKS" />
3、获取当前设备的MAC地址:
public static String getMacAddress(Context context) { String macAddress; WifiManager wifi = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); macAddress = info.getMacAddress(); if (null == macAddress) { return ""; } macAddress = macAddress.replace(":", ""); return macAddress;}
4、是否有SD卡:
public static boolean haveSDCard() {return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); }
5、主动回到Home,后台运行:
public static void goHome(Context context) { Intent mHomeIntent = new Intent(Intent.ACTION_MAIN); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); context.startActivity(mHomeIntent); }
6、获取状态栏高度:
注意,要在onWindowFocusChanged中调用,在onCreate中获取高度为0@TargetApi(Build.VERSION_CODES.CUPCAKE)public static int getStatusBarHeight(Activity activity) { Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); return frame.top; }
7、判断手机连接的网络类型(2G,3G,4G):
联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDOpublic class Constants { /** * Unknown network class */ public static final int NETWORK_CLASS_UNKNOWN = 0; /** * wifi net work */ public static final int NETWORK_WIFI = 1; /** * "2G" networks */ public static final int NETWORK_CLASS_2_G = 2; /** * "3G" networks */ public static final int NETWORK_CLASS_3_G = 3; /** * "4G" networks */ public static final int NETWORK_CLASS_4_G = 4;}public static int getNetWorkClass(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); switch (telephonyManager.getNetworkType()) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: return Constants.NETWORK_CLASS_2_G; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: return Constants.NETWORK_CLASS_3_G; case TelephonyManager.NETWORK_TYPE_LTE: return Constants.NETWORK_CLASS_4_G; default: return Constants.NETWORK_CLASS_UNKNOWN; } }
8、px-sp转换:
public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); }public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); }
9、手机号码正则:
public static final String REG_PHONE_CHINA = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
10、邮箱正则:
public static final String REG_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
更多相关文章
- Android(安卓)获取ip地址
- android 通过资源文件名称获取资源文件id
- Android如何获取asset目录下所有文件的路径
- android截取屏幕图
- Android(安卓)读写文件整理
- Android使用HttpURLConnection获取数据
- Android(安卓)MediaProjection学习(一)之和ImageReader实现屏幕截
- android:通过选择相册或者拍照获取照片
- android获取屏幕分辨率实现