update:2020.05.22
fix:toast显示问题,唤醒服务问题 add:
This commit is contained in:
@@ -9,9 +9,7 @@
|
||||
<uses-permission android:name="android.permission.MASTER_CLEAR" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
|
||||
<uses-permission android:name="android.permission.ACTION_SCREEN_ON" />
|
||||
<uses-permission android:name="android.permission.ACTION_SCREEN_OFF" />
|
||||
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||
<!--静默安装权限-->
|
||||
<uses-permission
|
||||
android:name="android.permission.INSTALL_PACKAGES"
|
||||
@@ -69,7 +67,16 @@
|
||||
<service
|
||||
android:name=".server.StepService"
|
||||
android:exported="true" />
|
||||
<service android:name=".server.InitJpushServer" />
|
||||
<service android:name=".server.InitJpushServer"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
<intent-filter android:priority="1000">
|
||||
<action android:name="android.intent.action.USER_PRESENT" />
|
||||
<action android:name="android.intent.action.SCREEN_ON" />
|
||||
<action android:name="android.intent.action.SCREEN_OFF" />
|
||||
<data android:scheme="package" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<receiver
|
||||
android:name=".receiver.NewAppReceiver"
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.mjsheng.myappstore;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
@@ -25,6 +28,9 @@ import com.mjsheng.myappstore.activity.MainActivity;
|
||||
import com.mjsheng.myappstore.comm.CommonDatas;
|
||||
import com.mjsheng.myappstore.jpush.TagAliasOperatorHelper;
|
||||
import com.mjsheng.myappstore.network.HTTPInterface;
|
||||
import com.mjsheng.myappstore.server.GuardService;
|
||||
import com.mjsheng.myappstore.server.InitJpushServer;
|
||||
import com.mjsheng.myappstore.server.MyDownloadService;
|
||||
import com.mjsheng.myappstore.utils.Configure;
|
||||
import com.mjsheng.myappstore.utils.MySQLData;
|
||||
import com.mjsheng.myappstore.utils.ToastUtil;
|
||||
@@ -109,7 +115,6 @@ public class MyApplication extends MultiDexApplication {
|
||||
.build()
|
||||
);
|
||||
|
||||
|
||||
// 设置开启日志,发布时请关闭日志
|
||||
JPushInterface.setDebugMode(true);
|
||||
JPushInterface.requestPermission(this);
|
||||
@@ -132,11 +137,57 @@ public class MyApplication extends MultiDexApplication {
|
||||
NetStateChangeReceiver.registerReceiver(this);
|
||||
|
||||
ToastUtil.init(this);
|
||||
if (!BuildConfig. DEBUG){
|
||||
if (!BuildConfig.DEBUG) {
|
||||
catchException();
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
//8.0之后需要在
|
||||
registerTimeReceiver();
|
||||
}
|
||||
}
|
||||
|
||||
private receiver receiver;
|
||||
|
||||
//监听时间和日期变化
|
||||
public void registerTimeReceiver() {
|
||||
receiver = new receiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(Intent.ACTION_USER_PRESENT);
|
||||
filter.addAction(Intent.ACTION_SCREEN_ON);
|
||||
filter.addAction(Intent.ACTION_SCREEN_OFF);
|
||||
filter.addAction(Intent.ACTION_USER_UNLOCKED);
|
||||
registerReceiver(receiver, filter);
|
||||
}
|
||||
|
||||
Long time1 = 0L;
|
||||
Long time2 = 0L;
|
||||
|
||||
public class receiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
Log.e("fht", "receiver action:" + action);
|
||||
if (action.equals(Intent.ACTION_USER_PRESENT)
|
||||
|| action.equals(Intent.ACTION_SCREEN_ON)
|
||||
|| action.equals(Intent.ACTION_SCREEN_OFF)
|
||||
) {
|
||||
time2 = System.currentTimeMillis();
|
||||
if ((time2 - time1) > 60 * 1000) {
|
||||
//可能会多次触发,1分钟之内不执行
|
||||
//application中启动服务,startcommand执行会两次
|
||||
startService(new Intent(context, InitJpushServer.class));
|
||||
startService(new Intent(context, GuardService.class));
|
||||
time1 = time2;
|
||||
Log.e("fht", "onReceive");
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onTagOperatorResult(JPushMessage jPushMessage) {
|
||||
if (jPushMessage == null) {
|
||||
|
||||
@@ -530,11 +530,8 @@ public class MainActivity extends AppCompatActivity implements NetStateChangeObs
|
||||
// fromNetToUpdate();
|
||||
String registrationID = JPushInterface.getRegistrationID(MainActivity.this);
|
||||
Log.e("mjsheng", registrationID + "------------------------");
|
||||
List<String> list = SaveListUtils.getList();
|
||||
if (list == null) {
|
||||
return;
|
||||
}
|
||||
LogUtils.e(list.toString() + "-----------------------------");
|
||||
SaveListUtils.getList();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1104,6 +1101,9 @@ public class MainActivity extends AppCompatActivity implements NetStateChangeObs
|
||||
} else {
|
||||
Aria.download(this).resumeAllTask();
|
||||
}
|
||||
case -200:
|
||||
boolean qch_force_app = Settings.System.putString(MainActivity.this.getContentResolver(), "qch_force_app", "invalid");
|
||||
Log.e("fht", "qch_force_app:" + qch_force_app);
|
||||
break;
|
||||
default:
|
||||
LogUtils.e("getForceDownload is error:" + forceDownloadBean.getMsg());
|
||||
@@ -1145,24 +1145,19 @@ public class MainActivity extends AppCompatActivity implements NetStateChangeObs
|
||||
return;
|
||||
}
|
||||
getSelfDownload(data);
|
||||
List<String> list = SaveListUtils.getList();
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
ForceDownloadData forceDownloadData = data.get(i);
|
||||
String app_package = forceDownloadData.getApp_package();
|
||||
String app_url = forceDownloadData.getApp_url();
|
||||
Log.e("fht ", "packageName=" + app_package + ",URL= " + app_url);
|
||||
|
||||
if (data.get(i).getApp_package().equals("com.jiaoguanyi.store")) {
|
||||
continue;//为自身的跳过下载
|
||||
}
|
||||
if (list != null) {
|
||||
if (!list.contains(app_package)) {
|
||||
SaveListUtils.addToList(app_package);
|
||||
}
|
||||
} else {
|
||||
SaveListUtils.addToList(app_package);
|
||||
if (!list.contains(app_package)) {
|
||||
list.add(app_package);
|
||||
}
|
||||
|
||||
if (!AppUtils.isAppInstalled(app_package)) {
|
||||
Log.e("fht ", "download URL " + app_url);
|
||||
// if (!SaveListUtils.isDownLoading(app_package)) {
|
||||
@@ -1175,6 +1170,8 @@ public class MainActivity extends AppCompatActivity implements NetStateChangeObs
|
||||
// }
|
||||
}
|
||||
}
|
||||
SaveListUtils.setList(list);
|
||||
SaveListUtils.sendForceAPP(this);
|
||||
}
|
||||
|
||||
private void getSelfDownload(List<ForceDownloadData> forceDownloadDataList) {
|
||||
|
||||
@@ -39,6 +39,7 @@ public class Network {
|
||||
|
||||
// public static final String ROOT_URL = "http://www.as.xueshibao.com.cn/api/"; 弃用
|
||||
// public static final String ROOT_URL = "https://www.jiaoguanyi.com/api/";//正式 弃用
|
||||
|
||||
// public static final String ROOT_URL = "https://partner.jiaoguanyi.com/api/";//正式
|
||||
// public static final String WebsocketURL = "ws://47.107.133.19:1234";
|
||||
public static final String ROOT_URL = "http://www.jiaoguanyi.cn/api/";//测试
|
||||
|
||||
@@ -17,10 +17,7 @@ public class BootReceiver extends BroadcastReceiver {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.e("BootReceiver", intent.getAction());
|
||||
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
|
||||
context.startService(new Intent(context, InitJpushServer.class));
|
||||
context.startService(new Intent(context, StepService.class));
|
||||
context.startService(new Intent(context, GuardService.class));
|
||||
context.startService(new Intent(context, MyDownloadService.class));
|
||||
startService(context);
|
||||
} else if (
|
||||
intent.getAction().equals("android.intent.action.BATTERY_CHANGED")
|
||||
|| intent.getAction().equals("android.intent.action.BATTERY_CHANGED")
|
||||
@@ -33,12 +30,17 @@ public class BootReceiver extends BroadcastReceiver {
|
||||
|| intent.getAction().equals("android.intent.action.SCREEN_ON")
|
||||
|| intent.getAction().equals("android.intent.action.SCREEN_OFF")
|
||||
) {
|
||||
Log.e("fht", "BootReceiver MSG" + intent.getAction());
|
||||
context.startService(new Intent(context, InitJpushServer.class));
|
||||
context.startService(new Intent(context, StepService.class));
|
||||
context.startService(new Intent(context, GuardService.class));
|
||||
context.startService(new Intent(context, MyDownloadService.class));
|
||||
//除了USER_PRESENT应该都收不到
|
||||
Log.e("fht", "BootReceiver MSG:" + intent.getAction());
|
||||
startService(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void startService(Context context) {
|
||||
context.startService(new Intent(context, InitJpushServer.class));
|
||||
context.startService(new Intent(context, StepService.class));
|
||||
context.startService(new Intent(context, GuardService.class));
|
||||
context.startService(new Intent(context, MyDownloadService.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,19 +295,10 @@ public class MyJPushReceiver extends BroadcastReceiver {
|
||||
String str = jSONObject.optString("app_url");
|
||||
Log.e("mymjsheng", "app_package::" + s);
|
||||
Log.e("mymjsheng", "app_url::" + str);
|
||||
SaveListUtils.addToList(s);
|
||||
List<String> list = SaveListUtils.getList();
|
||||
if (list != null) {
|
||||
if (!list.contains(s)) {
|
||||
SaveListUtils.addToList(s);
|
||||
}
|
||||
} else {
|
||||
SaveListUtils.addToList(s);
|
||||
}
|
||||
|
||||
SaveListUtils.addList(s);
|
||||
SaveListUtils.sendForceAPP(mContext);
|
||||
if (!AppUtils.isAppInstalled(s)) {
|
||||
if (!SaveListUtils.isDownLoading(s)) {
|
||||
|
||||
(Aria.download(this)
|
||||
.load(str)
|
||||
.setFilePath(PathUtils.getExternalDownloadsPath() + "/ygj/" + EncryptUtils.encryptMD5ToString(s) + ".apk").setExtendField(s))
|
||||
@@ -316,7 +307,6 @@ public class MyJPushReceiver extends BroadcastReceiver {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,7 +483,12 @@ public class MyJPushReceiver extends BroadcastReceiver {
|
||||
switch (message) {
|
||||
case MSG_DELETE:
|
||||
if (!TextUtils.isEmpty(title)) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if (SaveListUtils.getlist().contains(title)) {
|
||||
SaveListUtils.getlist().remove(title);
|
||||
}
|
||||
SaveListUtils.sendForceAPP(mContext);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
ApkUtils.uninstall(mContext, title);
|
||||
} else {
|
||||
ApkUtils.deleteApkInSilence(title);
|
||||
@@ -520,7 +515,7 @@ public class MyJPushReceiver extends BroadcastReceiver {
|
||||
settingPowerOn(extras);
|
||||
break;
|
||||
case MSG_RESET:
|
||||
Utils.doMasterClear(mContext);
|
||||
Utils.doMasterClear(mContext);
|
||||
break;
|
||||
case MSG_INSTALL:
|
||||
// mContext.sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
|
||||
|
||||
@@ -88,11 +88,18 @@ public class GuardService extends Service implements NetStateChangeObserver {
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
long time1 = 0L;
|
||||
long time2 = 0L;
|
||||
|
||||
//在这里处理任务执行中的状态,如进度进度条的刷新
|
||||
@Download.onTaskRunning
|
||||
protected void running(DownloadTask task) {
|
||||
Log.e("aria", "正在下载=--------------::" + task.getState() + "-------" + task.getPercent() + "-------" + task.getExtendField());
|
||||
ToastUtil.show("正在下载:" + task.getExtendField() + "--" + task.getPercent() + "%" + "\t" + Formatter.formatFileSize(GuardService.this, task.getSpeed()) + "/s");
|
||||
time2 = System.currentTimeMillis();
|
||||
if ((time2 - time1) > 3499) {
|
||||
ToastUtil.show("正在下载:" + task.getExtendField() + "--" + task.getPercent() + "%" + "\t" + Formatter.formatFileSize(GuardService.this, task.getSpeed()) + "/s");
|
||||
time1 = time2;
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskComplete
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.mjsheng.myappstore.server;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Handler;
|
||||
@@ -46,7 +49,9 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import cn.jpush.android.api.JPushInterface;
|
||||
@@ -84,6 +89,8 @@ public class InitJpushServer extends Service {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// 初始化 JPush
|
||||
@@ -91,12 +98,10 @@ public class InitJpushServer extends Service {
|
||||
timerImitate();
|
||||
// getNetworkState();
|
||||
Log.e("InitJpushServer", "onStartCommand");
|
||||
|
||||
// return super.onStartCommand(intent, flags, startId);
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
||||
private void initJpush() {
|
||||
TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
|
||||
tagAliasBean.action = ACTION_SET;
|
||||
@@ -134,7 +139,7 @@ public class InitJpushServer extends Service {
|
||||
|
||||
|
||||
//重新设置jpush
|
||||
private void timerImitate() {
|
||||
synchronized private void timerImitate() {
|
||||
Observable.timer(15000, TimeUnit.MILLISECONDS)
|
||||
.observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())
|
||||
.subscribe(new Action1<Long>() {
|
||||
@@ -382,6 +387,10 @@ public class InitJpushServer extends Service {
|
||||
Aria.download(this).resumeAllTask();
|
||||
}
|
||||
break;
|
||||
case -200:
|
||||
boolean qch_force_app = Settings.System.putString(InitJpushServer.this.getContentResolver(), "qch_force_app", "invalid");
|
||||
Log.e("fht", "qch_force_app:" + qch_force_app);
|
||||
break;
|
||||
default:
|
||||
LogUtils.e("getForceDownload is error:" + forceDownloadBean.getMsg());
|
||||
}
|
||||
@@ -404,8 +413,7 @@ public class InitJpushServer extends Service {
|
||||
return;
|
||||
}
|
||||
getSelfDownload(data);
|
||||
List<String> list = SaveListUtils.getList();
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
ForceDownloadData forceDownloadData = data.get(i);
|
||||
String app_package = forceDownloadData.getApp_package();
|
||||
@@ -413,13 +421,8 @@ public class InitJpushServer extends Service {
|
||||
if (data.get(i).getApp_package().equals("com.jiaoguanyi.appstore")) {
|
||||
continue;//为自身的跳过下载
|
||||
}
|
||||
LogUtils.e(list);
|
||||
if (list != null) {
|
||||
if (!list.contains(app_package)) {
|
||||
SaveListUtils.addToList(app_package);
|
||||
}
|
||||
} else {
|
||||
SaveListUtils.addToList(app_package);
|
||||
if (!list.contains(app_package)) {
|
||||
list.add(app_package);
|
||||
}
|
||||
if (!AppUtils.isAppInstalled(app_package)) {
|
||||
if (!SaveListUtils.isDownLoading(app_package)) {
|
||||
@@ -432,6 +435,8 @@ public class InitJpushServer extends Service {
|
||||
}
|
||||
}
|
||||
}
|
||||
SaveListUtils.setList(list);
|
||||
SaveListUtils.sendForceAPP(this);
|
||||
}
|
||||
|
||||
private void getSelfDownload(List<ForceDownloadData> forceDownloadDataList) {
|
||||
@@ -947,4 +952,10 @@ public class InitJpushServer extends Service {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
Log.e("fht", "InitJpushServer onDestroy");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public class InstallResultReceiver extends BroadcastReceiver {
|
||||
// Log.e("fht", SESSION_ID);
|
||||
// Log.e("fht", LEGACY_STATUS);
|
||||
// Log.e("fht", STATUS_MESSAGE);
|
||||
if (STATUS_MESSAGE.equals("INSTALL_SUCCEEDED")) {
|
||||
if (STATUS_MESSAGE != null && STATUS_MESSAGE.equals("INSTALL_SUCCEEDED")) {
|
||||
ToastUtil.show(PACKAGE_NAME + "安装成功");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.support.v4.content.FileProvider;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
@@ -42,17 +43,21 @@ import rx.schedulers.Schedulers;
|
||||
|
||||
public class SaveListUtils {
|
||||
private static List<String> downLoadList = new ArrayList<>();
|
||||
private static List<String> forceList = new ArrayList<>();
|
||||
|
||||
public static List<String> getList() {
|
||||
String data = SPStaticUtils.getString(CommonDatas.FORCE_INSTALL);
|
||||
Log.e("mjshenglistdata", data);
|
||||
|
||||
Gson gson = new Gson();
|
||||
Type listType = new TypeToken<List<String>>() {
|
||||
}.getType();
|
||||
List<String> list = gson.fromJson(data, listType);
|
||||
|
||||
// Log.e("mjshenglist1", list.toString());
|
||||
if (list != null && list.size() > 0) {
|
||||
Log.e("fht", "getList:" + list.toString());
|
||||
} else {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -69,6 +74,54 @@ public class SaveListUtils {
|
||||
String gsonData = gson.toJson(list);
|
||||
SPStaticUtils.put(CommonDatas.FORCE_INSTALL, gsonData);
|
||||
}
|
||||
Log.e("fht", "addToList:" + list.toString());
|
||||
}
|
||||
|
||||
public static void setList(List<String> strings) {
|
||||
forceList.clear();
|
||||
forceList = strings;
|
||||
Gson gson = new Gson();
|
||||
String gsonData = gson.toJson(forceList);
|
||||
SPStaticUtils.put(CommonDatas.FORCE_INSTALL, gsonData);
|
||||
Log.e("fht", "setList:" + forceList.toString());
|
||||
|
||||
}
|
||||
|
||||
public static void addList(String s) {
|
||||
if (!forceList.contains(s)) {
|
||||
forceList.add(s);
|
||||
}
|
||||
Log.e("fht", "addList:" + forceList.toString());
|
||||
|
||||
}
|
||||
|
||||
public static void setList() {
|
||||
Gson gson = new Gson();
|
||||
String gsonData = gson.toJson(forceList);
|
||||
SPStaticUtils.put(CommonDatas.FORCE_INSTALL, gsonData);
|
||||
}
|
||||
|
||||
|
||||
public static List<String> getlist() {
|
||||
return forceList;
|
||||
}
|
||||
|
||||
|
||||
public static void sendForceAPP(Context context) {
|
||||
String txt = "";
|
||||
for (String s : forceList) {
|
||||
txt += s + ",";
|
||||
}
|
||||
if (txt.length() > 0) {
|
||||
|
||||
txt = txt.substring(0, txt.length() - 1);
|
||||
boolean qch_force_app = Settings.System.putString(context.getContentResolver(), "qch_force_app", txt);
|
||||
Log.e("fht", "qch_force_app:" + qch_force_app + ":" + txt);
|
||||
} else {
|
||||
boolean qch_force_app = Settings.System.putString(context.getContentResolver(), "qch_force_app", "invalid");
|
||||
Log.e("fht", "qch_force_app:" + qch_force_app + ":" + txt);
|
||||
}
|
||||
setList();
|
||||
}
|
||||
|
||||
public static List<String> getDownLoadList() {
|
||||
@@ -79,7 +132,7 @@ public class SaveListUtils {
|
||||
public static void addDownLoadList(String packageName) {
|
||||
if (downLoadList == null) {
|
||||
downLoadList = new ArrayList<>();
|
||||
downLoadList.add(packageName);
|
||||
downLoadList.add(packageName);
|
||||
} else {
|
||||
if (!downLoadList.contains(packageName)) {
|
||||
downLoadList.add(packageName);
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.mjsheng.myappstore.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.mjsheng.myappstore.BuildConfig;
|
||||
@@ -14,13 +16,15 @@ import com.mjsheng.myappstore.BuildConfig;
|
||||
*/
|
||||
|
||||
public class ToastUtil {
|
||||
static Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
static Toast toast;
|
||||
private static Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private static Toast toast;
|
||||
private static Context mContext;
|
||||
|
||||
@SuppressLint("ShowToast")
|
||||
public static void init(Context context) {
|
||||
toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
|
||||
debugToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
|
||||
mContext = context;
|
||||
toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
|
||||
debugToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
|
||||
|
||||
}
|
||||
|
||||
@@ -28,12 +32,38 @@ public class ToastUtil {
|
||||
mainHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (toast != null) {
|
||||
toast.setText(msg);
|
||||
toast.show();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
showToast(mContext, msg, Toast.LENGTH_LONG);
|
||||
Log.e("fht", "LENGTH_LONG");
|
||||
|
||||
} else {
|
||||
if (toast != null) {
|
||||
toast.setText(msg);
|
||||
toast.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private static Toast mToast = null;
|
||||
|
||||
//android 8.0以后限制
|
||||
//https://www.jianshu.com/p/d9813ad03d59
|
||||
//https://www.jianshu.com/p/050ce052b873
|
||||
public static void showToast(Context context, String text, int duration) {
|
||||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
|
||||
Toast.makeText(context, text, duration).show();
|
||||
} else {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, text, duration);
|
||||
} else {
|
||||
mToast.setText(text);
|
||||
mToast.setDuration(duration);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
// public static void showInCenter(String msg) {
|
||||
|
||||
Reference in New Issue
Block a user