獲取手機信息的工具類PhoneHelper

PhoneHelper

獲取手機的一些基本信息,比如生產商家、固件版本、手機型號、手機號碼、屏幕分辨率以及震動、Toast提示、Notification等

代碼如下

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.Builder;
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.RemoteViews;
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;
        }
    }

    /**
     * 開始震動
     * 
     * @param context
     * @param repeat 0重複 -1不重複
     * @param pattern
     */
    @SuppressLint("NewApi")
    public synchronized void doVibrate(Context context, int repeat,
            long... pattern) {

        if (pattern == null) {
            pattern = new long[] { 1000, 1000, 1000 };
        }
        Vibrator mVibrator = (Vibrator) context
                .getSystemService(Context.VIBRATOR_SERVICE);
        if (mVibrator.hasVibrator()) {
            mVibrator.vibrate(pattern, repeat);
        }

    }

    /**
     * 建立一個NotificationCompat.Builder,並返回
     * @param contentTitle
     * @param contentText
     * @param contentInfo
     * @param largeIcon
     * @param smallIcon
     * @param contentclass
     * @return
     */
    public Builder getNotifyBuilder(String contentTitle, String contentText,
            String contentInfo, Bitmap largeIcon, int smallIcon,
            Class contentclass) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                new Intent(), 0);
        if (contentclass != null) {
            pendingIntent = PendingIntent.getActivity(context, 0, new Intent(
                    context, contentclass), 0);
        }
        Builder builder = new NotificationCompat.Builder(context)
                .setLargeIcon(largeIcon).setSmallIcon(smallIcon)
                .setContentInfo(contentInfo).setContentTitle(contentTitle)
                .setContentText(contentText).setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_ALL);
        return builder;

    }

    /**
     * 建一個帶有ProgressBar的NotificationCompat.Builder,並返回
     * @param contentTitle
     * @param contentText
     * @param contentInfo
     * @param largeIcon
     * @param smallIcon
     * @param contentclass
     * @param max
     * @param progress
     * @param indeterminate
     * @return
     */
    public Builder getNotifyBuilderProgress(String contentTitle,
            String contentText, String contentInfo, Bitmap largeIcon,
            int smallIcon, Class contentclass, int max, int progress,
            boolean indeterminate) {
        Builder builder = getNotifyBuilder(contentTitle, contentText,
                contentInfo, largeIcon, smallIcon, contentclass);

        builder.setProgress(max, progress, indeterminate);
        return builder;

    }

    /**
     * 普通的Notification
     * @param builder
     * @param notifyId
     */
    public void showNotifyNormal(Builder builder, int notifyId) {

        Notification notification = builder.build();

        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notifyId, notification);

    }

    /**
     * 大布局Notification
     * @param style
     * @param builder
     * @param notifyId
     */
    public void showNotifyBigView(NotificationCompat.Style style,
            Builder builder, int notifyId) {
        builder.setStyle(style);

        Notification notification = builder.build();

        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notifyId, notification);
    }

    /**
     * BigTextStyle風格的Notification
     * @param bigContentTitle
     * @param summaryText
     * @param bigText
     * @param builder
     * @param notifyId
     */
    public void showNotifyBigText(String bigContentTitle, String summaryText,
            String bigText, Builder builder, int notifyId) {

        NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
        textStyle.setBigContentTitle(bigContentTitle)
                .setSummaryText(summaryText).bigText(bigText);
        showNotifyBigView(textStyle, builder, notifyId);
    }

    /**
     * BigPictureStyle風格的Notification
     * @param bigContentTitle
     * @param summaryText
     * @param bigPicture
     * @param builder
     * @param notifyId
     */
    public void showNotifyBigPic(String bigContentTitle, String summaryText,
            Bitmap bigPicture, Builder builder, int notifyId) {

        NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
        pictureStyle.setBigContentTitle(bigContentTitle)
                .setSummaryText(summaryText).bigPicture(bigPicture);
        showNotifyBigView(pictureStyle, builder, notifyId);
    }

    /**
     * InboxStyle風格的Notification
     * @param bigContentTitle
     * @param summaryText
     * @param line
     * @param builder
     * @param notifyId
     */
    public void showNotifyBigInbox(String bigContentTitle, String summaryText,
            String[] line, Builder builder, int notifyId) {

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle(bigContentTitle).setSummaryText(
                summaryText);
        for (int i = 0; i < line.length; i++) {
            inboxStyle.addLine(line[i]);
        }

        showNotifyBigView(inboxStyle, builder, notifyId);
    }

    /**
     * 自定義Notification
     * @param layoutId
     * @param contentclass
     * @param smallIcon
     * @param clickCla
     * @param clickId
     * @param notifyId
     */
    public void showNotifyCustomView(int layoutId, Class contentclass,
            int smallIcon, Class clickCla, int clickId, int notifyId) {
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                layoutId);

        if (clickCla != null) {
            Intent intent = new Intent(context, clickCla);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, 0);
            remoteViews.setOnClickPendingIntent(clickId, pendingIntent);
        }

        Builder builder = getNotifyBuilder("", "", "", null, smallIcon,
                contentclass).setContent(remoteViews);
        manager.notify(notifyId, builder.build());
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章