获取Android手机信息的工具类PhoneHelper

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

获取手机的一些基本信息,比如生产商家、固件版本、手机型号、手机号码、屏幕分辨率等

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Toast;

public class PhoneHelper {
    private  Context context=App.getInstance();
    private static PhoneHelper util;
    public static PhoneHelper getInstance() {
        if (util == null) {
            util = new PhoneHelper();
        }
        return util;

    }

    private PhoneHelper() {
        super();
    }

    /**
     * 生产商家
     * 
     * @return
     */
    public String getManufacturer() {
        return android.os.Build.MANUFACTURER;
    }

    /**
     * 获得固件版本
     * 
     * @return
     */
    public String getRelease() {
        return android.os.Build.VERSION.RELEASE;
    }

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

    /**
     * 获得手机品牌
     * 
     * @return
     */
    public String getBrand() {
        return android.os.Build.BRAND;
    }

    /**
     * 获取手机运营商
     */
    public String getSimOperatorName() {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        return tm.getSimOperatorName(); 
    }

    /**
     * 得到本机手机号码,未安装SIM卡或者SIM卡中未写入手机号,都会获取不到
     * @return
     */
    public  String getThisPhoneNumber() {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String number = tm.getLine1Number();

        return  number; 
    }

    /**
     * 是否是电话号码
     * 
     * @param phonenumber
     * @return
     */
    public boolean isPhoneNumber(String phonenumber) {
        Pattern pa = Pattern.compile("^[1][3,4,5,8,7][0-9]{9}$");
        Matcher ma = pa.matcher(phonenumber);
        return ma.matches();
    }

    /**
     * 打电话
     * 
     * @param phone
     * @param context
     */
    public void doPhone(String phone) {
        Intent phoneIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                + phone));
        context.startActivity(phoneIntent);
    }

    /**
     * 发短信
     * 
     * @param phone
     * @param content
     * @param c
     */
    public void doSMS(String phone, String content) {
        Uri uri = null;
        if (!TextUtils.isEmpty(phone))
            uri = Uri.parse("smsto:" + phone);
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", content);
        context.startActivity(intent);
    }

    /**
     * 得到屏幕信息
     * getScreenDisplayMetrics().heightPixels 屏幕高
     * getScreenDisplayMetrics().widthPixels 屏幕宽
     * @return
     */
    public DisplayMetrics getScreenDisplayMetrics() {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        Display display = manager.getDefaultDisplay();
        display.getMetrics(displayMetrics);

        return displayMetrics;

    }

    /**
     * 屏幕分辨率
     * 
     * @param drame
     * @return
     */
    public float getDip() {

        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
                context.getResources().getDisplayMetrics());
    }

    /**
     * 安装apk
     */
    public void instance(File file) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 是否安装了
     * 
     * @param packageName
     * @return
     */
    public boolean isInstall(String packageName) {
        PackageManager packageManager = context.getPackageManager();
        List<ApplicationInfo> packs = packageManager
                .getInstalledApplications(PackageManager.GET_ACTIVITIES);
        for (ApplicationInfo info : packs) {
            if (info.packageName.equals(packageName))
                return true;
        }
        return false;
    }

    /**
     * 检测网络是否可用
     * 
     * @return
     */
    public boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnected();
    }

    /**
     * 将Toast放在屏幕上方
     * 
     * @param message
     */
    public void show(String message) {
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0,
                (getScreenDisplayMetrics().heightPixels / 5));
        toast.show();
    }

    /**
     * 调用浏览器打开
     * 
     * @param activity
     * @param url
     */
    public void openWeb(String url) {
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }


    /**
     * 是否有外存卡
     * 
     * @return
     */
    public boolean isExistExternalStore() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 得到sd卡路径
     * 
     * @return
     */
    public String getExternalStorePath() {
        if (isExistExternalStore()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }

    /**
     * 得到网络类型,0是未知或未连上网络,1为WIFI,2为2g,3为3g,4为4g
     * @return
     */
    public  int getNetType() {
        ConnectivityManager connectMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        int type = 0;
        NetworkInfo info = connectMgr.getActiveNetworkInfo();
        if (info == null||!info.isConnected()) {
            return type;
        }

        switch (info.getType()) {
        case ConnectivityManager.TYPE_WIFI:
            type = 1;
            break;
        case ConnectivityManager.TYPE_MOBILE:
            type = getNetworkClass(info.getSubtype());
            break;

        default:
            type=0;
            break;
        }

        return type;
    }

    /**
     * 判断数据连接的类型
     * @param networkType
     * @return
     */
    public int getNetworkClass(int networkType) {
        switch (networkType) {
        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:
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:

            return 2;
        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:
            return 3;
        case TelephonyManager.NETWORK_TYPE_LTE:
            return 4;
        default:
            return 0;
        }
    }
}

标签: isp 网络

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:ios实战-弹幕的实现

下一篇:iOS通过Http 上传图片