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(安卓)获取ip地址
  2. android 通过资源文件名称获取资源文件id
  3. Android如何获取asset目录下所有文件的路径
  4. android截取屏幕图
  5. Android(安卓)读写文件整理
  6. Android使用HttpURLConnection获取数据
  7. Android(安卓)MediaProjection学习(一)之和ImageReader实现屏幕截
  8. android:通过选择相册或者拍照获取照片
  9. android获取屏幕分辨率实现

随机推荐

  1. Android调用WCF
  2. Android Linux 内核介绍 (转)
  3. Android(安卓)项目实战视频资料 学习充电
  4. android网络编程——使用Android中的网络
  5. Android Studio:AndroidX的迁移
  6. Android 中自定义View(三)
  7. android app模拟 persistent 属性可以保
  8. android ndk 开发之 在 应用程序中使用 j
  9. [置顶] android调用第三方库——第二篇—
  10. Relativelayout的一些属性