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

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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;
        }
    }
}