Kaizige takes you to consolidate the application layer. Common code snippets necessary for beginners (2) Consolidate the application layer.

Source: Internet
Author: User

Kaizige takes you to consolidate the application layer. Common code snippets necessary for beginners (2) Consolidate the application layer.

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

The following content is from organizing multiple open-source projects and accumulating projects.

    • Collect device information for statistical analysis
    • SD card?
    • Dynamically hide a keyboard
    • Dynamic Display of keyboards
    • Dynamically display or hide a keyboard
    • Actively return to the Home background to run
    • Retrieve the Status Bar Height
    • Obtain the ActionBar height of the title bar of the status bar.
    • Get MCCMNC code SIM card carrier country code and carrier network code
    • Returns the name of the mobile network operator.
    • Return mobile terminal type
    • Determine the network type of the mobile phone connection.
    • Determine whether the network type of the current mobile phone is WIFI or 234 GB

Collect device information for statistical analysis
public static Properties collectDeviceInfo(Context context) {        Properties mDeviceCrashInfo = new Properties();        try {            PackageManager pm = context.getPackageManager();            PackageInfo pi = pm.getPackageInfo(context.getPackageName(),                    PackageManager.GET_ACTIVITIES);            if (pi != null) {                mDeviceCrashInfo.put(VERSION_NAME,                        pi.versionName == null ? "not set" : pi.versionName);                mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode);            }        } catch (PackageManager.NameNotFoundException e) {            Log.e(TAG, "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(TAG, "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("{\n");        for (Iterator iter = deviceInfos.iterator(); iter.hasNext();) {            Object item = iter.next();            deviceInfoStr.append("\t\t\t" + item + ":" + prop.get(item)                    + ", \n");        }        deviceInfoStr.append("}");        return deviceInfoStr.toString();    }
SD card?
public static boolean haveSDCard() {        return android.os.Environment.getExternalStorageState().equals(                android.os.Environment.MEDIA_MOUNTED);    }
Dynamically hide a keyboard
@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);    }
Dynamic Display of keyboards
@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);    }
Dynamically display or hide a keyboard
@TargetApi(Build.VERSION_CODES.CUPCAKE)public static void toggleSoftInput(Context context, EditText edit) {        edit.setFocusable(true);        edit.setFocusableInTouchMode(true);        edit.requestFocus();        InputMethodManager inputManager = (InputMethodManager) context                .getSystemService(Context.INPUT_METHOD_SERVICE);        inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);    }
Return to Home and run in the background
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);    }
Retrieve the Status Bar Height

Note: You must call it in onWindowFocusChanged to obtain a height of 0 in onCreate.

@TargetApi(Build.VERSION_CODES.CUPCAKE)public static int getStatusBarHeight(Activity activity) {    Rect frame = new Rect();    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);        return frame.top;    }
Obtain the height of the status bar + the height of the title bar (ActionBar)

(Note: If there is no ActionBar, the obtained height will be the same as the above, only the height of the status bar)

public static int getTopBarHeight(Activity activity) {        return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT)                .getTop();    }
Obtain the MCC + MNC Code (the country code of the SIM card carrier and the network code of the carrier)

Valid only when the user is registered on the network. CDMA may be invalid (China Mobile: 46000 46002, China Unicom: 46001, China Telecom: 46003)

public static String getNetworkOperator(Context context) {        TelephonyManager telephonyManager = (TelephonyManager) context                .getSystemService(Context.TELEPHONY_SERVICE);        return telephonyManager.getNetworkOperator();    }
Returns the name of the mobile network operator.

(For example, China Unicom, China Mobile, and China Telecom) is valid only when users are registered on the network. CDMA may be invalid)

public static String getNetworkOperatorName(Context context) {        TelephonyManager telephonyManager = (TelephonyManager) context                .getSystemService(Context.TELEPHONY_SERVICE);        return telephonyManager.getNetworkOperatorName();    }
Return mobile terminal type
public static int getPhoneType(Context context) {        TelephonyManager telephonyManager = (TelephonyManager) context                .getSystemService(Context.TELEPHONY_SERVICE);        return telephonyManager.getPhoneType();    }
Determine the network type of the mobile phone connection (2G, 3G, 4G)

China Unicom 3G is UMTS or HSDPA, China Mobile and China Unicom 2G is GPRS or EGDE, China Telecom 2G is CDMA, and China Telecom 3G is EVDO

public 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;        }    }
Determine the network type of the current mobile phone (WIFI or 2, 3, 4G)

The above method is required

public static int getNetWorkStatus(Context context) {        int netWorkType = Constants.NETWORK_CLASS_UNKNOWN;        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();        if (networkInfo != null && networkInfo.isConnected()) {            int type = networkInfo.getType();            if (type == ConnectivityManager.TYPE_WIFI) {                netWorkType = Constants.NETWORK_WIFI;            } else if (type == ConnectivityManager.TYPE_MOBILE) {                netWorkType = getNetWorkClass(context);            }        }        return netWorkType;    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.