Utils(下)一些常用的工具类

Utils(下)一些常用的工具类

1.App相关信息的工具类

/**
 * App工具类   
 * @author madreain
 */
public class AppUtil {

    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context)
    {
        try
        {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static String getVersionName(Context context)
    {
        try
        {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

}

2.软键盘工具类


/**
 * 键盘管理类   
 * @author madreain  
 */
public class SoftKeyboardManager {

    public static void HideSoftKeyboard(View v){
          /*隐藏软键盘*/
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
    }

    public static void HideSoftKeyboard(View v,Context context){
        //隐藏软键盘
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }


    public static  void ShowSoftKeyboard(View v,Context context){
        //软键盘弹出
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(v, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

}

3.网络工具类

/**
 * 网络工具类
 * @author madreain
 */
public class NetUtil {

    /**
     * 判断网络是否连接
     *
     * @param context
     * @return
     */
    public static boolean isConnected(Context context)
    {

        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (null != connectivity)
        {

            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (null != info && info.isConnected())
            {
                if (info.getState() == NetworkInfo.State.CONNECTED)
                {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是否是wifi连接
     */
    public static boolean isWifi(Context context)
    {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm == null)
            return false;
        return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;

    }

    /**
     * 打开网络设置界面
     */
    public static void openSetting(Activity activity)
    {
        Intent intent = new Intent("/");
        ComponentName cm = new ComponentName("com.android.settings",
                "com.android.settings.WirelessSettings");
        intent.setComponent(cm);
        intent.setAction("android.intent.action.VIEW");
        activity.startActivityForResult(intent, 0);
    }

}

4.自定义设置状态栏颜色工具类


/**
 * 状态栏颜色设置
 * @author madreain
 */
public class StatusBarManager {

    public static  void SetStatusBar(Window window,Context context,Resources resources,String color,ViewGroup layout){
        //状态栏  导航栏的开启
        if (android.os.Build.VERSION.SDK_INT > 18) {
//            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            layout.setPadding(0, getStatusBarHeight(resources), 0,
                    0);

        }
        //设置状态栏
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams lParams = new
                LinearLayout.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(resources));
        textView.setBackgroundColor(Color.parseColor(color));
        textView.setLayoutParams(lParams);
        // 获得根视图并把TextView加进去。
        ViewGroup viewGroup = (ViewGroup) window.getDecorView();
        viewGroup.addView(textView);
    }


    //    获取手机状态栏高度
    public static  int getStatusBarHeight(Resources resources) {
        Class c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
//            statusBarHeight = getResources().getDimensionPixelSize(x);
            statusBarHeight = resources.getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    // 获取ActionBar的高度

    public int getActionBarHeight(Resources resources) {
        TypedValue tv = new TypedValue();
        int actionBarHeight = 0;
//        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))//
                if (resources.newTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))//
//            如果资源是存在的、有效的
        {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.getDisplayMetrics());
        }
        return actionBarHeight;
    }

    //背景透明度的设置
    public  static  void backgroundAlpha(float bgAlpha,Window window ) {

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    }


}

具体使用设置使用

AndroidManifest.xml 设置activity

   <activity android:name=".ui.activity.SearchDeleteFriendsActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppSplash" />

AppSplash的代码块

  <style name="AppSplash" parent="AppTheme.NoActionBar">
        <!--<item name="android:windowBackground">@drawable/motobox_shape</item>-->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

具体布局最外层的LinearLayout(这里支持多种layout)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlayout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/M323535"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="com.motoband.ui.activity.SearchDeleteFriendsActivity">

在onCreate()方法setContentView后设置

  //状态栏颜色的设置
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout_main);
        StatusBarManager.SetStatusBar(getWindow(), this, getResources(), "#00312D", linearLayout);

大家有什么android开发问题可以一起交流(QQ:965244491 附微信二维码这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值