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+)*";

更多相关文章

  1. android 亮屏及屏幕解锁代码
  2. Android 从后台进入前台
  3. Android Service后台处理结果给Activity
  4. Android 实现Activity后台运行
  5. android屏幕解锁
  6. 2020年了,Android后台保活还有戏吗?看我如何优雅的实现!
  7. android 仿ios数字密码解锁界面
  8. Android 后台发邮件

随机推荐

  1. Android(安卓)“adb”不是内部或外部命令
  2. Android与服务器端数据交互
  3. android 编译自己的sdk
  4. [译] 在 Android 使用协程(part III) -
  5. android导入多个第三方包
  6. 实战技巧:Android异步指南
  7. Android 个别手机导航键覆盖布局解决办法
  8. Android开发中的MVC
  9. NFC:Arduino、Android与PhoneGap近场通信
  10. Android仿iPhone圆角边框