-
Notifications
You must be signed in to change notification settings - Fork 47
/
AbAppUtil.java
389 lines (360 loc) · 12.4 KB
/
AbAppUtil.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright (C) 2012 www.amsoft.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ab.util;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
/**
* 描述:应用工具类.
*/
public class AbAppUtil {
/**
* 读取application 节点 meta-data 信息
*/
public static String readMetaDataFromApplication(Context context, String key) {
try {
ApplicationInfo appInfo = context.getPackageManager()
.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
return appInfo.metaData.getString(key);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 描述:打开并安装文件.
*
* @param context the context
* @param file apk文件路径
*/
public static void installApk(Context context, 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 context the context
* @param packageName 包名
*/
public static void uninstallApk(Context context, String packageName) {
Intent intent = new Intent(Intent.ACTION_DELETE);
Uri packageURI = Uri.parse("package:" + packageName);
intent.setData(packageURI);
context.startActivity(intent);
}
/**
* 用来判断服务是否运行.
*
* @param ctx the ctx
* @param className 判断的服务名字 "com.xxx.xx..XXXService"
* @return true 在运行 false 不在运行
*/
public static boolean isServiceRunning(Context ctx, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
Iterator<RunningServiceInfo> l = servicesList.iterator();
while (l.hasNext()) {
RunningServiceInfo si = (RunningServiceInfo) l.next();
if (className.equals(si.service.getClassName())) {
isRunning = true;
}
}
return isRunning;
}
/**
* 停止服务.
*
* @param ctx the ctx
* @param className the class name
* @return true, if successful
*/
public static boolean stopRunningService(Context ctx, String className) {
Intent intent_service = null;
boolean ret = false;
try {
intent_service = new Intent(ctx, Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
}
if (intent_service != null) {
ret = ctx.stopService(intent_service);
}
return ret;
}
/**
* Gets the number of cores available in this device, across all processors.
* Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
*
* @return The number of cores, or 1 if failed to get result
*/
public static int getNumCores() {
try {
//Get directory containing CPU info
File dir = new File("/sys/devices/system/cpu/");
//Filter to only list the devices we care about
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//Check if filename is "cpu", followed by a single digit number
if (Pattern.matches("cpu[0-9]", pathname.getName())) {
return true;
}
return false;
}
});
//Return the number of cores (virtual CPU devices)
return files.length;
} catch (Exception e) {
//Default to return 1 core
return 1;
}
}
/**
* 描述:判断网络是否有效.
*
* @param context the context
* @return true, if is network available
*/
public static boolean isNetworkAvailable(Context context) {
try {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
/**
* Gps是否打开
* 需要<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />权限
*
* @param context the context
* @return true, if is gps enabled
*/
public static boolean isGpsEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
/**
* 判断当前网络是否是移动数据网络.
*
* @param context the context
* @return boolean
*/
public static boolean isMobile(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
/**
* 导入数据库.
*
* @param context the context
* @param dbName the db name
* @param rawRes the raw res
* @return true, if successful
*/
public static boolean importDatabase(Context context, String dbName, int rawRes) {
int buffer_size = 1024;
InputStream is = null;
FileOutputStream fos = null;
boolean flag = false;
try {
String dbPath = "/data/data/" + context.getPackageName() + "/databases/" + dbName;
File dbfile = new File(dbPath);
//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
if (!dbfile.exists()) {
//欲导入的数据库
if (!dbfile.getParentFile().exists()) {
dbfile.getParentFile().mkdirs();
}
dbfile.createNewFile();
is = context.getResources().openRawResource(rawRes);
fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[buffer_size];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.flush();
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
return flag;
}
/**
* 获取屏幕尺寸与密度.
*
* @param context the context
* @return mDisplayMetrics
*/
public static DisplayMetrics getDisplayMetrics(Context context) {
Resources mResources;
if (context == null) {
mResources = Resources.getSystem();
} else {
mResources = context.getResources();
}
//DisplayMetrics{density=1.5, width=480, height=854, scaledDensity=1.5, xdpi=160.421, ydpi=159.497}
//DisplayMetrics{density=2.0, width=720, height=1280, scaledDensity=2.0, xdpi=160.42105, ydpi=160.15764}
DisplayMetrics mDisplayMetrics = mResources.getDisplayMetrics();
return mDisplayMetrics;
}
/**
* 打开键盘.
*
* @param context the context
*/
public static void showSoftInput(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 关闭键盘事件.
*
* @param context the context
*/
public static void closeSoftInput(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null && ((Activity) context).getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* 获取包信息.
*
* @param context the context
*/
public static PackageInfo getPackageInfo(Context context) {
PackageInfo info = null;
try {
String packageName = context.getPackageName();
info = context.getPackageManager().getPackageInfo(packageName, 0);
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
/**
* 获取当前版本号
*
* @return
* @throws Exception
*/
public static String getVersionName(Context context) {
StringBuilder builder = new StringBuilder("v");
try {
PackageInfo packInfo = getPackageInfo(context);
builder.append(packInfo.versionName);
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
/**
* 获取开发版本号
*
* @param context
* @return
*/
public static String getVersionCode(Context context) {
StringBuilder builder = new StringBuilder();
try {
PackageInfo packInfo = getPackageInfo(context);
builder.append(packInfo.versionCode);
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
/**
* 是否存在该包名的应用
*
* @param context
* @return
*/
public static boolean exitAppBy(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (pn.equals(packageName)) {
return true;
}
}
}
return false;
}
}