-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAppUtils.java
127 lines (115 loc) · 3.23 KB
/
AppUtils.java
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
import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import java.util.Arrays;
import java.util.List;
/**
* AppUtils
*
* @author: onlylemi
*/
public class AppUtils {
private AppUtils() {
}
public static void share(Context context, String text) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.putExtra(Intent.EXTRA_SUBJECT, "好友分享");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, "分享 - 知乎日报"));
}
/**
* list 转 string
*
* @param list
* @param spaceCharacter
* @return
*/
public static String list2String(List<String> list, String spaceCharacter) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i));
if (i != list.size() - 1) {
sb.append(spaceCharacter);
}
}
return sb.toString();
}
/**
* string 转 list
*
* @param str
* @param spaceCharacter
* @return
*/
public static List<String> string2List(String str, String spaceCharacter) {
return Arrays.asList(str.split(spaceCharacter));
}
/**
* dp 转 px
*
* @param context
* @param dp
* @return
*/
public static int dp2px(Context context, int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
context.getResources().getDisplayMetrics());
}
/**
* 获取缓存目录
*
* @param context
* @param folder
* @return
*/
public static File getDiskCacheDir(Context context, String folder) {
String cacheDir;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
!Environment.isExternalStorageRemovable()) {
cacheDir = context.getExternalCacheDir().getPath();
} else {
cacheDir = context.getCacheDir().getPath();
}
return new File(cacheDir + File.separator + folder);
}
/**
* 获取应用版本号
*
* @param context
* @return
*/
public static int getAppVersion(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName
(), 0);
return info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
/**
* 屏幕的width
*
* @param activity
* @return
*/
public static int screeWidth(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
/**
* 屏幕的高
*
* @param activity
* @return
*/
public static int screeHeight(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.heightPixels;
}
}