Android常用的几个系统参数相关代码片段

1.收集设备信息,用于信息统计分析

public static Properties collectDeviceInfo(Context context) { 
        Properties mDeviceCrashInfo = new Properties();
        try { 
            PackageManager pm = context.getPackageManager(); 
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES); 
            if (packageInfo != packageInfo) { 
                mDeviceCrashInfo.put("VERSION_NAME", packageInfo.versionName == null ? "not set" : packageInfo.versionName); 
                mDeviceCrashInfo.put("VERSION_CODE", packageInfo.versionCode); 
            } 
        } catch (PackageManager.NameNotFoundException e) { 
            Log.e("demo0606", "Error while collect package info", e); 
        }
        Field[] fields = Build.class.getDeclaredFields(); 
        for (Field field : fields) { 
            try { field.setAccessible(true); 
            mDeviceCrashInfo.put(field.getName(), field.get(null)); 
            } catch (Exception e) { 
                Log.e("demo0606", "Error while collect crash info", e); 
            }
        } return mDeviceCrashInfo; 
    } 
    public static String collectDeviceInfoStr(Context context) { 
        Properties prop = collectDeviceInfo(context); 
        Set deviceInfos = prop.keySet(); 
        StringBuilder deviceInfoStr = new StringBuilder(""); 
        for (Iterator iter = deviceInfos.iterator(); iter.hasNext();) { 
            Object item = iter.next(); deviceInfoStr.append( item + ":" + prop.get(item) + "\n"); 
        } 
        return deviceInfoStr.toString(); 
    }

2.是否有SD卡

    public static boolean haveSDCard() { 
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
    }

3.动态隐藏软键盘

    @TargetApi(Build.VERSION_CODES.CUPCAKE) 
    public static void hideSoftInput(Activity activity) { 
        View view = activity.getWindow().peekDecorView();
        if (view != null) { 
            InputMethodManager inputmanger = (InputMethodManager) activity .getSystemService(Context.INPUT_METHOD_SERVICE); 
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0); 
        }
    } 
    @TargetApi(Build.VERSION_CODES.CUPCAKE) 
    public static void hideSoftInput(Context context, EditText edit) {
        edit.clearFocus(); 
        InputMethodManager inputmanger = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputmanger.hideSoftInputFromWindow(edit.getWindowToken(), 0);
    }

4动态显示软键盘

@TargetApi(Build.VERSION_CODES.CUPCAKE)
    public static void showSoftInput(Context context, EditText edit) { 
        edit.setFocusable(true); edit.setFocusableInTouchMode(true); 
        edit.requestFocus(); 
        InputMethodManager inputManager = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.showSoftInput(edit, 0);
    }

5.回到桌面,程序后台运行

    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.获取状态栏和标题栏高度(注意,如果没有ActionBar,那么获取的高度将和上面的是一样的,只有状态栏的高度)

public static int getTopBarHeight(Activity activity) { 
        return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT) .getTop(); 
    }

7.获取MCC+MNC代码 (SIM卡运营商国家代码和运营商网络代码) 仅当用户已在网络注册时有效, CDMA 可能会无效
(中国移动:46000 46002, 中国联通:46001,中国电信:46003)

public static String getNetworkOperator(Context context) { 
        TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); 
        return telephonyManager.getNetworkOperator(); 
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值