Tool.java
4.26 KB
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
package com.studymachine.www.other;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.text.TextUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.OnHttpListener;
import com.studymachine.www.app.AppApplication;
import com.studymachine.www.http.api.UpdateVersionApi;
import com.studymachine.www.http.model.HttpData;
import com.studymachine.www.ui.dialog.UpdateDialog;
public class Tool {
/**
* 拼接url
*
* @param url
* @param path
* @return
*/
public static String joinUrl(String url, String path) {
if (path == null) {
return "";
}
return url.replaceAll("\\/+$", "") +
"/" +
path.replaceAll("^\\/+", "");
}
/**
* 拼接 oss url
*/
public static String jointOSSPath(String url) {
return joinUrl(AppConfig.getImagerHostUrl(), url);
}
//播放音频
public static MediaPlayer playMediaPlayer(Context context, int id) {
MediaPlayer mediaPlayer = MediaPlayer.create(context, id);
mediaPlayer.setLooping(false);//是否循环播放 true是 false否
mediaPlayer.start();
return mediaPlayer;
}
public static boolean isApkInstalled(Context context, String packageName) {
if (TextUtils.isEmpty(packageName)) {
return false;
}
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static void startOtherApp(Context context, String packageName) {
final Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(intent);
}
//关闭音频
public static void closeMediaPlayer(MediaPlayer mediaPlayer) {
if (mediaPlayer != null) {
try {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
} catch (Exception e) {
}
mediaPlayer.release();
mediaPlayer = null;
}
}
/**
* 版本检测
*
* @param context 上下文
*/
private static UpdateDialog.Builder updateDialog;
public static void updateVersion(Context context) {
EasyHttp.post(new ApplicationLifecycle())
.api(new UpdateVersionApi())
.request(new OnHttpListener<HttpData<UpdateVersionApi.Bean>>() {
@Override
public void onSucceed(HttpData<UpdateVersionApi.Bean> result) {
UpdateVersionApi.Bean bean = result.getData();
if (bean != null && bean.getUrl() != null) {
if (updateDialog == null || updateDialog.getContext() != context) {
updateDialog = new UpdateDialog.Builder(context);
updateDialog.setVersionName(bean.getVersionValue())
.setForceUpdate(!"1".equals(bean.getForceUpdate()))
.setUpdateLog(bean.getRemark())
.setDownloadUrl(bean.getUrl())
.show();
}
// new UpdateDialog.Builder(context)
// .setVersionName(bean.getVersionValue())
// .setForceUpdate(!"1".equals(bean.getForceUpdate()))
// .setUpdateLog(bean.getRemark())
// .setDownloadUrl(bean.getUrl())
// .show();
}
}
@Override
public void onFail(Exception e) {
}
});
}
}