Android开发模板------常用工具类(方法)

Android常用工具类(方法)大全


<span style="font-size:14px;">package com.example.thinkpad.recyclerviewdemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Utils {
    /**
     * 获取手机及SIM卡相关信息
     * @param context
     * @return
     */
    public static Map<String,String> getPhoneInfo(Context context){
        Map<String,String> map = new HashMap<String,String>();
        TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();
        String imsi = tm.getSubscriberId();
        String phoneMode = Build.MODEL;
        String phoneSDK = Build.VERSION.RELEASE;
        map.put("imei",imei);
        map.put("imsi",imsi);
        map.put("phoneMode",phoneMode+"##"+phoneSDK);
        map.put("model",phoneMode);
        map.put("sdk",phoneSDK);
        return map;
    }

    /**
     * 根据手机的分辨率从dp的单位转成px(像素)
     * @param context
     * @param dpValue
     * @return
     */
    public static int dp2px(Context context,float dpValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率px(像素)从的单位转成dp
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dp(Context context, float pxValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue/scale + 0.5f);
    }

    /**
     * 安装 .apk文件
     * @param context
     * @param fileName
     * 下面两句可以不要:
     * intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     * intent.setAction(Intent.ACTION_VIEW);
     */
    public void install(Context context,String fileName){
        if (TextUtils.isEmpty(fileName) || context == null){
            return;;
        }

        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(fileName)),
                    "application/vnd.android.package-archive");
            context.startActivity(intent);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 检查某程序是否安装
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isInstalledApp(Context context,String packageName){
        boolean flag = false;
        try {
            PackageManager pm = context.getPackageManager();
            List<PackageInfo> pkgs = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
            for (PackageInfo pkg:pkgs){
                if ((pkg.packageName).equals(packageName)){
                    return flag = true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 关闭系统的软键盘
     * @param activity
     */
    public static void dismissSoftKeyboard(Activity activity){
        View view = activity.getWindow().peekDecorView();
        if (view != null){
            InputMethodManager inputmanager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanager.hideSoftInputFromInputMethod(view.getWindowToken(),0);
        }
}
</span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值