擷取手機資訊的工具類PhoneHelper,工具類phonehelper

來源:互聯網
上載者:User

擷取手機資訊的工具類PhoneHelper,工具類phonehelper
PhoneHelper

擷取手機的一些基本資料,比如生產商家、韌體版本、手機型號、手機號碼、螢幕解析度等

代碼如下
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;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.