【android 安卓开发】老程常用安卓开发方法【待整理】

版权声明:✎ 本文为博主原创文章,未经博主(老程)允许不得转载。➤ https://blog.csdn.net/weixin_41000111/article/details/79647430

前言

在苦逼的公司,什么都让你上,及时不是你的专业,也得你上。因为便宜货,技术还不好,只能坚持干,今天就分享下载工作中常用的一些方法吧。先搁着,等项目一完成,就开始整理

<uses-permission android:name="android.permission.INTERNET"/>
 <!--允许读取网络状态-->
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!--允许读取wifi网络状态-->
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
package com.zaidayou.newcs;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Toast;

import java.util.List;
import java.util.Locale;

/**
 * Created by cheng on 2018/3/18.
 */

public class publics {
    // 弹窗
    public static void Toast(Context _this,String txt){
        Toast.makeText(_this,txt,Toast.LENGTH_LONG).show();
    }

    /**
     * 获取当前手机系统语言。
     *
     * @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
     */
    public static String getSystemLanguage() {
        return Locale.getDefault().getLanguage();
    }

    /**
     * 获取当前系统上的语言列表(Locale列表)
     *
     * @return  语言列表
     */
    public static Locale[] getSystemLanguageList() {
        return Locale.getAvailableLocales();
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }

    /**
     * 判断网路状态(是否离线状态)
     *
     * @return  网路
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
        } else {
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    /**
     * 获取当期的GPS是否打开
     *
     * @return  网路
     */
    public static boolean isGpsEnabled(Context context) {
        LocationManager lm = ((LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE));
        List<String> accessibleProviders = lm.getProviders(true);
        return accessibleProviders != null && accessibleProviders.size() > 0;
    }
    /**
     * WIFI是否打开
     *
     * @return  网路
     */
    public static boolean isWifiEnabled(Context context) {
        ConnectivityManager mgrConn = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager mgrTel = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
                .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
                .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
    }
    /**
     * 判断是否是3G网路
     *
     * @return  网路
     */
    public static boolean is3rd(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        if (networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }
    /**
     * 判断是wifi还是3g网络。(wifi就可以建议下载或者在线播放)
     *
     * @return  网路
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        if (networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }

    /**
     * 数据保存,只能存真假。
     *
     * @return  数据
     */
    public static void datasBoolean (Context context,String key,boolean valBoolean) {
        // 得到SharedPreferences
        SharedPreferences preferences = context.getSharedPreferences(
                "preference", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, valBoolean);
        editor.commit();
    }
    /**
     * 数据保存,字符串。
     *
     * @return  数据
     */
    public static void dataString (Context context,String key,String val) {
        // 得到SharedPreferences
        SharedPreferences preferences = context.getSharedPreferences(
                "preference", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, val);
        editor.commit();
    }
    /**
     * 数据保存,数字。
     *
     * @return  数据
     */
    public static void dataInt (Context context,String key,int val) {
        // 得到SharedPreferences
        SharedPreferences preferences = context.getSharedPreferences(
                "preference", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, val);
        editor.commit();
    }
    /**
     * 数据获取,只能返回真假
     *
     * @return  数据
     */
    public static boolean getBoolean(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(
                "preference", Context.MODE_PRIVATE);
        // 返回key值,key值默认值是false
        return preferences.getBoolean(key, false);
    }
    /**
     * 返回数据。返回int类型
     *
     * @return  数据
     */
    public static int getInt(Context context,String key){
        SharedPreferences preferences = context.getSharedPreferences(
                "preference", Context.MODE_PRIVATE);
        // 返回key值,key值默认值是0
        return preferences.getInt(key, 0);
    }
    public static String getDataString(Context context, String key){
        SharedPreferences preferences = context.getSharedPreferences(
                "preference", Context.MODE_PRIVATE);
        // 返回key值,key值默认值是0
        return preferences.getString(key, "");
    }
    /**
     * 获取屏幕的高度
    * */
    public static void getHeight(Context context){
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        final int width = dm.widthPixels;         // 屏幕宽度(像素)
        final int height = dm.heightPixels;       // 屏幕高度(像素)
        float density = dm.density;         // 屏幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi(120 / 160 / 240)
        // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
        final int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        final int screenHeight = (int) (height / density);// 屏幕高度(dp)
        // 方法内不能套用方法,只能类里面套方法。
        class getHeight_class {
            public int getScreenWidth() {
                return screenWidth;
            }
            public int getScreenHeight() {
                return screenHeight;
            }
            public int get_width() {
                return width;
            }
            public int get_height() {
                return height;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41000111/article/details/79647430