这里需要用到一个jar包,下载拿到这个jar包后直接双击就可以生成市场大部分主流屏幕尺寸了。然后只要把生成好的xml尺寸文件拷贝到相应的value文件中即可。很方便,以后再也不用担心适配繁琐的问题了。
这里提供下载地址:https://pan.baidu.com/s/1geRpCcj
相关工具类:
1 package com.investmenthelp;
2
3 import android.annotation.SuppressLint;
4 import android.annotation.TargetApi;
5 import android.app.Activity;
6 import android.app.ActivityManager;
7 import android.content.ClipData;
8 import android.content.ClipboardManager;
9 import android.content.ComponentName;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.content.pm.PackageInfo;
13 import android.content.pm.PackageManager;
14 import android.content.pm.ResolveInfo;
15 import android.content.res.Resources;
16 import android.database.Cursor;
17 import android.net.ConnectivityManager;
18 import android.net.NetworkInfo;
19 import android.net.Uri;
20 import android.os.Environment;
21 import android.os.StatFs;
22 import android.os.Vibrator;
23 import android.provider.MediaStore;
24 import android.telephony.TelephonyManager;
25 import android.text.TextUtils;
26 import android.util.DisplayMetrics;
27 import android.view.View;
28 import android.view.ViewTreeObserver;
29 import android.view.Window;
30 import android.view.inputmethod.InputMethodManager;
31
32 import java.io.File;
33 import java.net.InetAddress;
34 import java.net.NetworkInterface;
35 import java.net.SocketException;
36 import java.util.ArrayList;
37 import java.util.Enumeration;
38 import java.util.List;
39 import java.util.Map;
40
41 /**
42 * Created by 火龙裸先生 on 2017/6/7 0007.
43 * Desction:设备相关工具类
44 */
45
46 public class DeviceUtils {
47 /**
48 * Unknown network class
49 */
50 public static final int NETWORK_CLASS_UNKNOWN = 0;
51
52 /**
53 * wifi net work
54 */
55 public static final int NETWORK_WIFI = 1;
56
57 /**
58 * "2G" networks
59 */
60 public static final int NETWORK_CLASS_2_G = 2;
61
62 /**
63 * "3G" networks
64 */
65 public static final int NETWORK_CLASS_3_G = 3;
66
67 /**
68 * "4G" networks
69 */
70 public static final int NETWORK_CLASS_4_G = 4;
71
72 /**
73 * 判断SDCard是否可用
74 */
75 public static boolean existSDCard() {
76 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
77 return true;
78 } else {
79 return false;
80 }
81 }
82
83 /**
84 * 获取本机IP地址
85 *
86 * @return
87 */
88 public static String getLocalIPAddress() {
89
90 try {
91 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
92 NetworkInterface intf = en.nextElement();
93 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
94 InetAddress inetAddress = enumIpAddr.nextElement();
95 if (!inetAddress.isLoopbackAddress()) {
96 return inetAddress.getHostAddress().toString();
97 }
98 }
99 }
100 } catch (SocketException ex) {
101 return "0.0.0.0";
102 }
103 return "0.0.0.0";
104 }
105
106 /**
107 * 多个SD卡时 取外置SD卡
108 *
109 * @return
110 */
111 public static String getExternalStorageDirectory() {
112 // 参考文章
113 //
114 Map<String, String> map = System.getenv();
115 String[] values = new String[map.values().size()];
116 map.values().toArray(values);
117 String path = values[values.length - 1];
118 if (path.startsWith("/mnt/") && !Environment.getExternalStorageDirectory().getAbsolutePath().equals(path)) {
119 return path;
120 } else {
121 return null;
122 }
123 }
124
125 /**
126 * 获取可用空间大小
127 *
128 * @return
129 */
130 public static long getAvailaleSize() {
131 if (!existSDCard()) {
132 return 0l;
133 }
134 File path = Environment.getExternalStorageDirectory(); //取得sdcard文件路径
135 StatFs stat = new StatFs(path.getPath());
136 long blockSize = stat.getBlockSize();
137 long availableBlocks = stat.getAvailableBlocks();
138 return availableBlocks * blockSize;
139 }
140
141 /**
142 * 获取SD大小
143 *
144 * @return
145 */
146 public static long getAllSize() {
147 if (!existSDCard()) {
148 return 0l;
149 }
150 File path = Environment.getExternalStorageDirectory();
151 StatFs stat = new StatFs(path.getPath());
152 long blockSize = stat.getBlockSize();
153 long availableBlocks = stat.getBlockCount();
154 return availableBlocks * blockSize;
155 }
156
157 public static boolean isOnline(Context context) {
158 try {
159 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
160 NetworkInfo ni = cm.getActiveNetworkInfo();
161 return ni != null ? ni.isConnectedOrConnecting() : false;
162 } catch (Exception e) {
163 e.printStackTrace();
164 return false;
165 }
166 }
167
168 /**
169 * 服务是否运行
170 *
171 * @param mContext
172 * @param className
173 * @return
174 */
175 public static boolean isServiceRunning(Context mContext, String className) {
176 boolean isRunning = false;
177 ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
178 List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
179 if (serviceList.size() == 0) {
180 return false;
181 }
182 for (int i = 0; i < serviceList.size(); i++) {
183 if (serviceList.get(i).service.getClassName().equals(className) == true) {
184 isRunning = true;
185 break;
186 }
187 }
188 return isRunning;
189 }
190
191 /**
192 * 进程是否运行
193 */
194 public static boolean isProessRunning(Context context, String proessName) {
195 boolean isRunning = false;
196 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
197
198 List<ActivityManager.RunningAppProcessInfo> lists = am.getRunningAppProcesses();
199 for (ActivityManager.RunningAppProcessInfo info : lists) {
200 if (info.processName.equals(proessName)) {
201 isRunning = true;
202 return isRunning;
203 }
204 }
205
206 return isRunning;
207 }
208
209 /**
210 * 震动
211 *
212 * @param context
213 * @param duration
214 */
215 public static void vibrate(Context context, long duration) {
216 Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
217 long[] pattern = {0, duration};
218 vibrator.vibrate(pattern, -1);
219 }
220
221 /**
222 * 获取最后一次拍照的图片
223 *
224 * @param context
225 * @return
226 */
227 public static String getLatestCameraPicture(Context context) {
228
229 if (!existSDCard()) {
230 return null;
231 }
232
233 String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns
234 .BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.DATE_TAKEN, MediaStore.Images.ImageColumns.MIME_TYPE};
235 Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images
236 .ImageColumns.DATE_TAKEN + " DESC");
237 if (cursor.moveToFirst()) {
238 String path = cursor.getString(1);
239 return path;
240 }
241 return null;
242 }
243
244 /**
245 * 获取手机大小(分辨率)
246 *
247 * @param activity
248 * @return
249 */
250 public static DisplayMetrics getScreenPix(Activity activity) {
251 // DisplayMetrics 一个描述普通显示信息的结构,例如显示大小、密度、字体尺寸
252 DisplayMetrics displaysMetrics = new DisplayMetrics();
253 // 获取手机窗口的Display 来初始化DisplayMetrics 对象
254 // getManager()获取显示定制窗口的管理器。
255 // 获取默认显示Display对象
256 // 通过Display 对象的数据来初始化一个DisplayMetrics 对象
257 activity.getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics);
258 return displaysMetrics;
259 }
260
261 /**
262 * 复制到剪切板
263 *
264 * @param context
265 * @param content
266 */
267 @TargetApi(11)
268 public static void coptyToClipBoard(Context context, String content) {
269 int currentapiVersion = android.os.Build.VERSION.SDK_INT;
270 if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
271 ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
272 ClipData clip = ClipData.newPlainText("label", content);
273 clipboard.setPrimaryClip(clip);
274 } else {
275 android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
276 clipboard.setText(content);
277 }
278 }
279
280 /**
281 * 获取非系统应用包名
282 *
283 * @param context
284 * @return
285 */
286 public static List<String> getAppPackageNamelist(Context context) {
287 List<String> packList = new ArrayList<String>();
288 PackageManager pm = context.getPackageManager();
289 List<PackageInfo> packinfos = pm.getInstalledPackages(0);
290 for (PackageInfo packinfo : packinfos) {
291 String packname = packinfo.packageName;
292 packList.add(packname);
293 }
294
295 return packList;
296 }
297
298 /**
299 * 判断某个应用是否已经安装
300 *
301 * @param context 上下文
302 * @param packageName 包名
303 * @return 是否已经安装
304 */
305 public static boolean isAppInstall(Context context, String packageName) {
306 // 获取packagemanager
307 final PackageManager packageManager = context.getPackageManager();
308 // 获取所有已安装程序的包信息
309 List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
310 // 用于存储所有已安装程序的包名
311 List<String> packageNames = new ArrayList<String>();
312 // 从pinfo中将包名字逐一取出,压入pName list中
313 if (packageInfos != null) {
314 for (int i = 0; i < packageInfos.size(); i++) {
315 String packName = packageInfos.get(i).packageName;
316 packageNames.add(packName);
317 }
318 }
319 // 判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE
320 return packageNames.contains(packageName);
321 }
322
323 public static int dip2px(Context context, float dipValue) {
324 final float scale = context.getResources().getDisplayMetrics().density;
325 return (int) (dipValue * scale + 0.5f);
326 }
327
328 public static int px2dip(Context context, float pxValue) {
329 final float scale = context.getResources().getDisplayMetrics().density;
330 return (int) (pxValue / scale + 0.5f);
331 }
332
333 /**
334 * 判断是否有软控制键(手机底部几个按钮)
335 *
336 * @param activity
337 * @return
338 */
339 public boolean isSoftKeyAvail(Activity activity) {
340 final boolean[] isSoftkey = {false};
341 final View activityRootView = (activity).getWindow().getDecorView().findViewById(android.R.id.content);
342 activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
343 @Override
344 public void onGlobalLayout() {
345 int rootViewHeight = activityRootView.getRootView().getHeight();
346 int viewHeight = activityRootView.getHeight();
347 int heightDiff = rootViewHeight - viewHeight;
348 if (heightDiff > 100) { // 99% of the time the height diff will be due to a keyboard.
349 isSoftkey[0] = true;
350 }
351 }
352 });
353 return isSoftkey[0];
354 }
355
356 /**
357 * 获取statusbar高度
358 *
359 * @param context
360 * @return
361 */
362 public static int getStatusBarHeight(Context context) {
363 int height = 0;
364 int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
365 if (resourceId > 0) {
366 height = context.getResources().getDimensionPixelSize(resourceId);
367 }
368
369 return height;
370 }
371
372 /**
373 * 获取navigationbar高度
374 *
375 * @param context
376 * @return
377 */
378 public static int getNavigationBarHeight(Context context) {
379 int height = 0;
380 Resources resources = context.getResources();
381 int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
382 //获取NavigationBar的高度
383 if (resourceId > 0) {
384 height = resources.getDimensionPixelSize(resourceId);
385 }
386 return height;
387 }
388
389 /**
390 * 获取状态栏高度+标题栏(ActionBar)高度
391 * (注意,如果没有ActionBar,那么获取的高度将和上面的是一样的,只有状态栏的高度)
392 *
393 * @param activity
394 * @return
395 */
396 public static int getTopBarHeight(Activity activity) {
397 return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
398 }
399
400
401 /**
402 * @param @param context
403 * @param @param packageName 包名
404 * @return void 返回类型
405 * @throws
406 * @Title: startActivityForPackage
407 * @Description: TODO(通过)
408 */
409 @SuppressLint("NewApi")
410 public static boolean startActivityForPackage(Context context, String packageName) {
411 PackageInfo pi = null;
412 try {
413 pi = context.getPackageManager().getPackageInfo(packageName, 0);
414 } catch (PackageManager.NameNotFoundException e) {
415 e.printStackTrace();
416 return false;
417 }
418
419 Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
420 resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
421 resolveIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
422 // resolveIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
423 resolveIntent.setPackage(pi.packageName);
424
425 List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(resolveIntent, 0);
426
427 ResolveInfo ri = apps.iterator().next();
428 if (ri != null) {
429 String packageName1 = ri.activityInfo.packageName;
430 String className = ri.activityInfo.name;
431
432 Intent intent = new Intent(Intent.ACTION_MAIN);
433 intent.addCategory(Intent.CATEGORY_LAUNCHER);
434 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
435
436 ComponentName cn = new ComponentName(packageName1, className);
437
438 intent.setComponent(cn);
439 context.startActivity(intent);
440 return true;
441 }
442 return false;
443 }
444
445 /**
446 * 隐藏键盘
447 * :强制隐藏
448 *
449 * @param context
450 */
451 public static void hideInputSoftFromWindowMethod(Context context, View view) {
452 try {
453 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
454 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
455 } catch (Exception e) {
456 e.printStackTrace();
457 }
458 }
459
460 /**
461 * 显示输入法
462 *
463 * @param context
464 * @param view
465 */
466 public static void showInputSoftFromWindowMethod(Context context, View view) {
467 try {
468 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
469 imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
470 } catch (Exception e) {
471 e.printStackTrace();
472 }
473 }
474
475 /**
476 * 判断输入负是否处于激活状态
477 *
478 * @param context
479 * @return
480 */
481 public static boolean isActiveSoftInput(Context context) {
482 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
483 return imm.isActive();
484 }
485
486 /**
487 * 主动回到Home,后台运行
488 *
489 * @param context
490 */
491 public static void goHome(Context context) {
492 Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
493 mHomeIntent.addCategory(Intent.CATEGORY_HOME);
494 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
495 context.startActivity(mHomeIntent);
496 }
497
498 /**
499 * 返回移动终端类型
500 * PHONE_TYPE_NONE :0 手机制式未知
501 * PHONE_TYPE_GSM :1 手机制式为GSM,移动和联通
502 * PHONE_TYPE_CDMA :2 手机制式为CDMA,电信
503 * PHONE_TYPE_SIP:3
504 *
505 * @param context
506 * @return
507 */
508 public static int getPhoneType(Context context) {
509 TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
510 return telephonyManager.getPhoneType();
511 }
512
513 /**
514 * 判断手机连接的网络类型(wifi,2G,3G,4G)
515 * 联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO
516 *
517 * @param context
518 * @return
519 */
520 public static int getNetType(Context context) {
521 int netWorkType = NETWORK_CLASS_UNKNOWN;
522
523 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
524 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
525
526 if (networkInfo != null && networkInfo.isConnected()) {
527 int type = networkInfo.getType();
528
529 if (type == ConnectivityManager.TYPE_WIFI) {
530 netWorkType = NETWORK_WIFI;
531 } else if (type == ConnectivityManager.TYPE_MOBILE) {
532 TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
533 switch (telephonyManager.getNetworkType()) {
534 case TelephonyManager.NETWORK_TYPE_GPRS:
535 case TelephonyManager.NETWORK_TYPE_EDGE:
536 case TelephonyManager.NETWORK_TYPE_CDMA:
537 case TelephonyManager.NETWORK_TYPE_1xRTT:
538 case TelephonyManager.NETWORK_TYPE_IDEN:
539 return NETWORK_CLASS_2_G;
540 case TelephonyManager.NETWORK_TYPE_UMTS:
541 case TelephonyManager.NETWORK_TYPE_EVDO_0:
542 case TelephonyManager.NETWORK_TYPE_EVDO_A:
543 case TelephonyManager.NETWORK_TYPE_HSDPA:
544 case TelephonyManager.NETWORK_TYPE_HSUPA:
545 case TelephonyManager.NETWORK_TYPE_HSPA:
546 case TelephonyManager.NETWORK_TYPE_EVDO_B:
547 case TelephonyManager.NETWORK_TYPE_EHRPD:
548 case TelephonyManager.NETWORK_TYPE_HSPAP:
549 return NETWORK_CLASS_3_G;
550
551 case TelephonyManager.NETWORK_TYPE_LTE:
552 return NETWORK_CLASS_4_G;
553 default:
554 return NETWORK_CLASS_UNKNOWN;
555 }
556 }
557 }
558
559 return netWorkType;
560 }
561
562 /**
563 * 跳转至拨号界面
564 *
565 * @param context
566 * @param phoneNumber 电话号码电话号码
567 */
568 public static void callDial(Context context, String phoneNumber) {
569 context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
570 }
571
572 /**
573 * 发送短信
574 *
575 * @param context
576 * @param phoneNumber
577 * @param content
578 */
579 public static void sendSms(Context context, String phoneNumber, String content) {
580 Uri uri = Uri.parse("smsto:" + (TextUtils.isEmpty(phoneNumber) ? "" : phoneNumber));
581 Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
582 intent.putExtra("sms_body", TextUtils.isEmpty(content) ? "" : content);
583 context.startActivity(intent);
584 }
585
586 /**
587 * 判断当前设备是否为手机
588 *
589 * @param context
590 * @return
591 */
592 public static boolean isPhone(Context context) {
593 TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
594 if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
595 return false;
596 } else {
597 return true;
598 }
599 }
600 }