Android常用检查判断方法(本人忘性大留个备份没事看看)

来源:互联网 发布:淘宝的权重是什么意思 编辑:程序博客网 时间:2024/04/29 00:18

这是我在网上找的一些判断检测。自己属于过目即忘的那种所以这些小东西还是留下来万一用得到了呢?你说是吧

import android.app.ActivityManager;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.Environment;import android.telephony.TelephonyManager;import java.io.File;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by single on. */public class CheckUtils {    /**     * 判断字符串是否为Null或""     *     * @param str     * @return true=>Null或"";false=>不为Null或""     */    public static boolean strIsNull(String str) {        if (null != str && !"".equals(str))            return false;        return true;    }    /**     * 判断字符串是否为正整数     * +1, +2, +3……算作正整数     * 0,1.0,2.0,3.0……不算做正整数     *     * @param str     * @return true=>是;false=>不是     */    public static boolean strIsPositiveInteger(String str) {        if (strIsNull(str))            return false;        Pattern p = Pattern.compile("[1-9]\\d*");        Pattern p1 = Pattern.compile("\\+[1-9]\\d*");        Matcher m = p.matcher(str);        Matcher m1 = p1.matcher(str);        if (m.matches() || m1.matches())            return true;        return false;    }    /**     * 判断字符串是否为负整数     * 0,-1.0,-2.0,-3.0……不算做负整数     *     * @param str     * @return true=>是;false=>不是     */    public static boolean strIsNegativeInteger(String str) {        if (strIsNull(str))            return false;        Pattern p = Pattern.compile("-[1-9]\\d*");        Matcher m = p.matcher(str);        if (m.matches())            return true;        return false;    }    /**     * 判断字符串是否是整数     * +xxx,xxx,-xxx,+0,-0,0都算作整数     *     * @param str     * @return true=>是;false=>不是     */    public static boolean strIsInteger(String str) {        if (strIsNull(str))            return false;        Pattern p = Pattern.compile("((-|\\+)?[1-9]\\d*)|((-|\\+)?0)");        Matcher m = p.matcher(str);        if (m.matches())            return true;        return false;    }    /**     * 判断字符串是否为正浮点型(Float or Double)数据     * x.xx或+x.xx都算作正浮点数     * 0.0不算做正浮点数     *     * @param str     * @return true=>是;false=>不是     */    public static boolean strIsPositiveFloat(String str) {        if (strIsNull(str))            return false;        if (strIsPositiveInteger(str) || strIsNegativeInteger(str))            return false;        Pattern p = Pattern.compile("[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*");        Pattern p1 = Pattern.compile("\\+([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)");        Matcher m = p.matcher(str);        Matcher m1 = p1.matcher(str);        if (m.matches() || m1.matches())            return true;        return false;    }    /**     * 判断字符串是否为负浮点型(Float or Double)数据     * -0.0不算做负浮点数     *     * @param str     * @return true=>是;false=>不是     */    public static boolean strIsNegativeFloat(String str) {        if (strIsNull(str))            return false;        if (strIsPositiveInteger(str) || strIsNegativeInteger(str))            return false;        Pattern p = Pattern.compile("-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)");        Matcher m = p.matcher(str);        if (m.matches())            return true;        return false;    }    /**     * 判断字符串是否为数字     *     * @param str     * @return true=>是;false=>不是     */    public static boolean strIsNumber(String str) {        if (strIsNull(str))            return false;        if (strIsInteger(str) || strIsNegativeFloat(str) || strIsPositiveFloat(str))            return true;        return false;    }    /**     * 当前是否有网络连接     *     * @param context     * @return     */    public static boolean isNetwork(Context context) {        if (null != context) {            ConnectivityManager connectivityManager = (ConnectivityManager) context                    .getSystemService(Context.CONNECTIVITY_SERVICE);            NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();            return networkInfo != null && networkInfo.isConnected();        }        return false;    }    /**     * 获取网络连接类型     *     * @param context     * @return     */    public static int getNetworkType(Context context) {        if (null != context) {            ConnectivityManager connectivityManager = (ConnectivityManager) context                    .getSystemService(Context.CONNECTIVITY_SERVICE);            NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();            return networkInfo == null ? -1 : networkInfo.getType();        }        return -1;    }    /**     * 获取网络连接类型名称     * disconnect:断开连接,wifi:WIFI,eg:高速移动网络(3G),2g,wap,wnknown:未知     *     * @param context     * @return     */    public static String getNetworkTypeName(Context context) {        if (null != context) {            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);            NetworkInfo networkInfo;            String type = "disconnect";            if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) {                return type;            }            if (networkInfo.isConnected()) {                String typeName = networkInfo.getTypeName();                if ("WIFI".equalsIgnoreCase(typeName)) {                    type = "wifi";                } else if ("MOBILE".equalsIgnoreCase(typeName)) {                    String proxyHost = android.net.Proxy.getDefaultHost();                    type = strIsNull(proxyHost) ? (isFastMobileNetwork(context) ? "eg" : "2g")                            : "wap";                } else {                    type = "unknown";                }            }        }        return "unknown";    }    /**     * 是否是快速移动网络     *     * @param context     * @return     */    public static boolean isFastMobileNetwork(Context context) {        if (null != context) {            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);            if (telephonyManager == null) {                return false;            }            switch (telephonyManager.getNetworkType()) {                case TelephonyManager.NETWORK_TYPE_1xRTT:                    return false;                case TelephonyManager.NETWORK_TYPE_CDMA:                    return false;                case TelephonyManager.NETWORK_TYPE_EDGE:                    return false;                case TelephonyManager.NETWORK_TYPE_EVDO_0:                    return true;                case TelephonyManager.NETWORK_TYPE_EVDO_A:                    return true;                case TelephonyManager.NETWORK_TYPE_GPRS:                    return false;                case TelephonyManager.NETWORK_TYPE_HSDPA:                    return true;                case TelephonyManager.NETWORK_TYPE_HSPA:                    return true;                case TelephonyManager.NETWORK_TYPE_HSUPA:                    return true;                case TelephonyManager.NETWORK_TYPE_UMTS:                    return true;                case TelephonyManager.NETWORK_TYPE_EHRPD:                    return true;                case TelephonyManager.NETWORK_TYPE_EVDO_B:                    return true;                case TelephonyManager.NETWORK_TYPE_HSPAP:                    return true;                case TelephonyManager.NETWORK_TYPE_IDEN:                    return false;                case TelephonyManager.NETWORK_TYPE_LTE:                    return true;                case TelephonyManager.NETWORK_TYPE_UNKNOWN:                    return false;                default:                    return false;            }        }        return false;    }    /**     * 判断当前应用是否运行在前台     *     * @param context     * @return true 前台     */    public static boolean isForeground(Context context) {        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {            if (appProcess.processName.equals(context.getPackageName())) {                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {                    return false;                } else {                    return true;                }            }        }        return true;    }    /**     * 检查SD卡是否存在     *     * @return     */    public static boolean isExistSDCard() {        if (Environment.getExternalStorageState().equals(                Environment.MEDIA_MOUNTED)) {            return true;        } else {            return false;        }    }    /**     * 检查文件是否存在     *     * @param path     * @return     */    public static boolean isExistFile(String path) {        try {            File f = new File(path);            if (!f.exists()) {                return false;            }        } catch (Exception e) {            return false;        }        return true;    }    /**     * 是否是手机号码     *     * @param mobile     * @return true:是     */    public static boolean isMobile(String mobile) {        Pattern p = Pattern                .compile("^((14[0-9])|(17[0-9])|(13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");        Matcher m = p.matcher(mobile);        return m.matches();    }    /**     * 是否是固定电话     *     * @param phone     * @return true:是     */    public static boolean isLandlineTelePhone(String phone) {        String str = "^((\\d{3,4}\\-)|)\\d{7,8}(|([-\\u8f6c]{1}\\d{1,5}))$";        Pattern p = Pattern.compile(str);        Matcher m = p.matcher(phone);        return m.matches();    }    /**     * 是否是邮政编码     *     * @param zipCode     * @return true:是     */    public static boolean isZipCode(String zipCode) {        String str = "^[1-9]\\d{5}$";        Pattern p = Pattern.compile(str);        Matcher m = p.matcher(zipCode);        return m.matches();    }    /**     * 是否是邮箱     *     * @param email     * @return true:是     */    public static boolean isEmail(String email) {        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";        Pattern p = Pattern.compile(str);        Matcher m = p.matcher(email);        return m.matches();    }    /**     * 一般昵称规则验证     * 是否由汉字字符数字下划线组成     *     * @param nick     * @return true:是     */    public static boolean isNickname(String nick) {        String str = "[\u4e00-\u9fa5_a-zA-Z0-9_]+";        Pattern p = Pattern.compile(str);        Matcher m = p.matcher(nick);        return m.matches();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
0 0