update:2021.03.17
fix:更改获取接口的触发条件,网络访问失败时使用本地的数据
add:
This commit is contained in:
FHT
2021-03-17 17:04:40 +08:00
parent 6a03350550
commit d3b6bf3bed
12 changed files with 220 additions and 76 deletions

View File

@@ -41,8 +41,8 @@ android {
productFlavors {
official {
flavorDimensions "default"
versionCode 1032
versionName "2.0.3.2"// 正式jiaoguanyi.com 双数正式 单数测试
versionCode 1033
versionName "2.0.3.3"// 正式jiaoguanyi.com 双数正式 单数测试
/*********************************极光推送************************************/
manifestPlaceholders = [
JPUSH_PKGNAME: "com.jiaoguanyi.appstore",

View File

@@ -3,8 +3,6 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.mjsheng.myappstore"
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
@@ -42,6 +40,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

View File

@@ -2,18 +2,23 @@ package com.mjsheng.myappstore;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.nfc.Tag;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.os.Process;
import android.provider.Settings;
import androidx.multidex.MultiDexApplication;
import android.text.TextUtils;
import android.util.Log;
@@ -40,7 +45,6 @@ import com.mjsheng.myappstore.jpush.TagAliasOperatorHelper;
import com.mjsheng.myappstore.network.HTTPInterface;
import com.mjsheng.myappstore.network.Network;
import com.mjsheng.myappstore.network.api.newapi.GetLockState;
import com.mjsheng.myappstore.receiver.BootReceiver;
import com.mjsheng.myappstore.receiver.NewAppReceiver;
import com.mjsheng.myappstore.server.GuardService;
import com.mjsheng.myappstore.server.InitJpushServer;
@@ -48,7 +52,9 @@ import com.mjsheng.myappstore.server.MyDownloadService;
import com.mjsheng.myappstore.server.StepService;
import com.mjsheng.myappstore.utils.Configure;
import com.mjsheng.myappstore.utils.MySQLData;
import com.mjsheng.myappstore.utils.NetworkUtils;
import com.mjsheng.myappstore.utils.SPUtils;
import com.mjsheng.myappstore.utils.SystemUtils;
import com.mjsheng.myappstore.utils.ToastUtil;
import com.mjsheng.myappstore.utils.Utils;
import com.mjsheng.myappstore.utils.update.ToastTool;
@@ -91,9 +97,8 @@ import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
//public class MyApplication extends MultiDexApplication implements Thread.UncaughtExceptionHandler {
public class MyApplication extends MultiDexApplication {
private final String TAG = MyApplication.class.getSimpleName();
public static String userName = null;
private static final String TAG = "--MyApplication--";
public static Context context;
public static boolean LOOP_STOPING = false; //停止 循环
public static MainActivity mainActivity;
@@ -125,7 +130,12 @@ public class MyApplication extends MultiDexApplication {
@Override
public void onCreate() {
super.onCreate();
if (SystemUtils.IsMainProcessName(this, Process.myPid())) {
init();
}
}
private void init() {
Configuration config = getResources().getConfiguration();
int smallestScreenWidthDp = config.smallestScreenWidthDp;
Log.e("mjsheng", "smallestScreenWidthDp=" + smallestScreenWidthDp);
@@ -175,6 +185,8 @@ public class MyApplication extends MultiDexApplication {
startService(new Intent(this, MyDownloadService.class));
}
registerTimeReceiver();
registerBatteryReceiver();
registerNetworkReceiver();
//忽略电池优化
// ignoreBatteryOptimization(this);
@@ -230,7 +242,6 @@ public class MyApplication extends MultiDexApplication {
}
/**
* 忽略电池优化
*/
@@ -266,7 +277,7 @@ public class MyApplication extends MultiDexApplication {
//监听时间和日期变化
public void registerTimeReceiver() {
receiver = new receiver();
screenReceiver = new ScreenReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(Intent.ACTION_USER_PRESENT);
@@ -275,10 +286,10 @@ public class MyApplication extends MultiDexApplication {
filter.addAction(Intent.ACTION_USER_UNLOCKED);
filter.addAction("android.intent.action.FACTORY_RESET");
filter.addAction("android.intent.action.MASTER_CLEAR");
registerReceiver(receiver, filter);
registerReceiver(screenReceiver, filter);
}
private receiver receiver;
private ScreenReceiver screenReceiver;
Long time1 = 0L;
Long time2 = 0L;
@@ -286,12 +297,12 @@ public class MyApplication extends MultiDexApplication {
Long time3 = 0L;
Long time4 = 0L;
public class receiver extends BroadcastReceiver {
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
Log.e("fht", "receiver action:" + action);
Log.e(TAG, "action:" + action);
if (action.equals(Intent.ACTION_SCREEN_ON)) {
long time = System.currentTimeMillis();
if (null != locationClient) {
@@ -299,52 +310,55 @@ public class MyApplication extends MultiDexApplication {
locationClient.startLocation();
}
getLockState("2", String.valueOf(time));
} else if (action.equals(Intent.ACTION_USER_PRESENT)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/*在8.0以上 除了开机广播其他广播基本上没有用
*当静默安装升级时app不会被唤醒只有通过显示广播唤醒
* */
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED);
intent1.setComponent(new ComponentName("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver"));
// 携带数据
// intent1.putExtra("test", "我是来测 A 应用的Android 8.0 系统静态广播的测试数据");
// 启动发送广播
sendBroadcast(intent1);
time2 = System.currentTimeMillis();
if ((time2 - time1) > 60 * 1000) {
//可能会多次触发1分钟之内不执行
//application中启动服务,startcommand执行会两次
startService(new Intent(context, InitJpushServer.class));
startService(new Intent(context, StepService.class));
startService(new Intent(context, GuardService.class));
time1 = time2;
}
}
}, 5000);
}
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
long time = System.currentTimeMillis();
getLockState("1", String.valueOf(time));
if (time3 - System.currentTimeMillis() >= 10000) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED);
intent1.setComponent(new ComponentName("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver"));
sendBroadcast(intent1);
deleteScreenshots();
}
}
time3 = System.currentTimeMillis();
}
}, 10000);
} else if (action.equals("android.intent.action.FACTORY_RESET")
}
// else if (action.equals(Intent.ACTION_USER_PRESENT)) {
//// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//// /*在8.0以上 除了开机广播其他广播基本上没有用
//// *当静默安装升级时app不会被唤醒只有通过显示广播唤醒
//// * */
//// mHandler.postDelayed(new Runnable() {
//// @Override
//// public void run() {
//// Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED);
//// intent1.setComponent(new ComponentName("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver"));
//// // 携带数据
////// intent1.putExtra("test", "我是来测 A 应用的Android 8.0 系统静态广播的测试数据");
//// // 启动发送广播
//// sendBroadcast(intent1);
////
//// time2 = System.currentTimeMillis();
//// if ((time2 - time1) > 60 * 1000) {
//// //可能会多次触发1分钟之内不执行
//// //application中启动服务,startcommand执行会两次
//// startService(new Intent(context, InitJpushServer.class));
//// startService(new Intent(context, StepService.class));
//// startService(new Intent(context, GuardService.class));
//// time1 = time2;
//// }
//// }
//// }, 5000);
////
//// }
//// }
//// else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
//// mHandler.postDelayed(new Runnable() {
//// @Override
//// public void run() {
//// long time = System.currentTimeMillis();
//// getLockState("1", String.valueOf(time));
//// if (time3 - System.currentTimeMillis() >= 10000) {
//// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//// Intent intent1 = new Intent(BootReceiver.BOOT_COMPLETED);
//// intent1.setComponent(new ComponentName("com.jiaoguanyi.store", "com.mjsheng.myappstore.receiver.BootReceiver"));
//// sendBroadcast(intent1);
//// deleteScreenshots();
//// }
//// }
//// time3 = System.currentTimeMillis();
//// }
//// }, 10000);
//// }
else if (action.equals("android.intent.action.FACTORY_RESET")
|| action.equals("android.intent.action.MASTER_CLEAR")
//MASTER_CLEAR 7.0可以监听
|| action.equals("android.intent.action.MASTER_CLEAR_NOTIFICATION")
@@ -358,6 +372,83 @@ public class MyApplication extends MultiDexApplication {
}
private void registerBatteryReceiver() {
if (null == batteryReceiver) {
batteryReceiver = new BatteryReceiver();
}
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_OKAY);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
registerReceiver(batteryReceiver, filter);
}
BatteryReceiver batteryReceiver;
private class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
// 当前电量
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
// 最大电量
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
int elec = (level * 100) / scale;
Log.e(TAG, "electricity:=" + elec + "%");
if (elec == 80 || elec == 50 || elec == 30) {
startService(new Intent(getBaseContext(), InitJpushServer.class));
}
} else if (Intent.ACTION_POWER_CONNECTED.equals(action)
|| Intent.ACTION_POWER_DISCONNECTED.equals(action)
|| Intent.ACTION_BATTERY_LOW.equals(action)
|| Intent.ACTION_BATTERY_OKAY.equals(action)
) {
Log.e(TAG, action);
startService(new Intent(getBaseContext(), InitJpushServer.class));
}
}
}
private void registerNetworkReceiver() {
if (null == netReceiver) {
netReceiver = new NetworkStateReceiver();
}
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.ethernet.ETHERNET_STATE_CHANGED");
filter.addAction("android.net.ethernet.STATE_CHANGE");
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filter.addAction("android.net.wifi.STATE_CHANGE");
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(netReceiver, filter);
}
private NetworkStateReceiver netReceiver;
public class NetworkStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || intent.getAction() == null) {
Log.e(TAG, "NetworkStateReceiver: 异常");
return;
}
if (NetworkUtils.isConnected(getBaseContext())) {
Log.e(TAG, "NetworkStateReceiver: 网络连接成功");
startService(new Intent(getBaseContext(), InitJpushServer.class));
} else {
Log.e(TAG, "NetworkStateReceiver: 网络连接失败");
}
}
}
private void deleteScreenshots() {
Log.e("File", "deleteScreenshots");
String path = getExternalFilesDir("db").getAbsolutePath();
@@ -535,7 +626,7 @@ public class MyApplication extends MultiDexApplication {
}
}
synchronized public static void setJpushAlias() {
synchronized public static void setJpushAlias() {
Log.e("jiguangInterface", "10s后重新设置alias");
Observable.timer(10000, TimeUnit.MILLISECONDS)
.observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())

View File

@@ -870,6 +870,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
public void onError(Call call, Response response, Exception e) {
super.onError(call, response, e);
Log.e("onError", "error:" + e.getMessage());
handler.sendEmptyMessage(-2);
}
});// 请求方式和请求url

View File

@@ -12,11 +12,12 @@ import com.mjsheng.myappstore.server.MyDownloadService;
import com.mjsheng.myappstore.server.StepService;
public class BootReceiver extends BroadcastReceiver {
private String TAG = BootReceiver.class.getSimpleName();
public static final String BOOT_COMPLETED = "com.jiaoguanyi.appstore.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BootReceiver", intent.getAction());
Log.e(TAG, "action:" + intent.getAction());
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
HTTPInterface.updateDeviceInfo(context);
startService(context);
@@ -31,7 +32,7 @@ public class BootReceiver extends BroadcastReceiver {
|| intent.getAction().equals("android.intent.action.USER_PRESENT")
|| intent.getAction().equals("android.intent.action.SCREEN_ON")
|| intent.getAction().equals("android.intent.action.SCREEN_OFF")
//测试
//测试
// || intent.getAction().equals("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")
// || intent.getAction().equals("android.hardware.usb.action.USB_ACCESSORY_DETACHED")
// || intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_ATTACHED")
@@ -46,15 +47,15 @@ public class BootReceiver extends BroadcastReceiver {
) {
//除了USER_PRESENT应该都收不到
Log.e("fht", "BootReceiver MSG:" + intent.getAction());
startService(context);
// Log.e("fht", "BootReceiver MSG:" + intent.getAction());
// startService(context);
}
}
private void startService(Context context) {
Intent intent=new Intent(context, InitJpushServer.class);
Intent intent = new Intent(context, InitJpushServer.class);
// intent.putExtra("boot","BOOT_COMPLETED");
context.startService(intent);
context.startService(new Intent(context, StepService.class));

View File

@@ -363,7 +363,7 @@ public class MyJPushReceiver extends BroadcastReceiver {
}
private void setLock_screen(int state, Context context, String name) {
if (ServiceAliveUtils.isServiceAlice()) {
if (ServiceAliveUtils.isServiceAlice(context)) {
context.startService(new Intent(context, InitJpushServer.class));
}
Intent intent = new Intent();

View File

@@ -64,7 +64,7 @@ public class GuardService extends Service {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
LogUtils.e(TAG, "GuardService:建立链接");
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice(GuardService.this);
if (!isServiceRunning) {
Intent i = new Intent(GuardService.this, MyDownloadService.class);
startService(i);

View File

@@ -50,7 +50,7 @@ public class StepService extends Service {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
LogUtils.e(TAG, "StepService:建立链接");
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice(StepService.this);
if (!isServiceRunning) {
Intent i = new Intent(StepService.this, MyDownloadService.class);
startService(i);

View File

@@ -670,12 +670,12 @@ public class ApkUtils {
//10.0上日历和电子邮件是可卸载的
//7.0是系统应用
if (show_canremove_systemapp.contains(packageInfo.packageName)) {
Logger.e("showAllAPP2", "packageName:" + packageInfo.packageName);
// Logger.e("showAllAPP2", "packageName:" + packageInfo.packageName);
pm.setApplicationEnabledSetting(packageInfo.packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
}
}
} else {
Logger.e("showAllAPP", "packageName:" + packageInfo.packageName);
// Logger.e("showAllAPP", "packageName:" + packageInfo.packageName);
pm.setApplicationEnabledSetting(packageInfo.packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
hideSystemSettingAPP(context, packageInfo.packageName);
}

View File

@@ -0,0 +1,29 @@
package com.mjsheng.myappstore.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;
public class NetworkUtils {
public static boolean isConnected(Context context) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= 23) {
//获取网络属性
NetworkCapabilities networkCapabilities = mConnectivityManager.getNetworkCapabilities(mConnectivityManager.getActiveNetwork());
if (networkCapabilities != null) {
Log.e("Avalible", "NetworkCapalbilities:" + networkCapabilities.toString());
return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
} else {
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isConnected();
}
}
return false;
}
}

View File

@@ -7,20 +7,19 @@ import android.util.Log;
import com.mjsheng.myappstore.MyApplication;
public class ServiceAliveUtils {
public static boolean isServiceAlice() {
public static boolean isServiceAlice(Context mContext) {
boolean isServiceRunning = false;
ActivityManager manager =
(ActivityManager) MyApplication.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
(ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
if (manager == null) {
return false;
}
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.mjsheng.myappstore.server.InitJpushServer".equals(service.service.getClassName())) {
if (mContext.getClass().getName().equals(service.service.getClassName())) {
isServiceRunning = true;
}
}
Log.e("ServiceAliveUtils", "isServiceAlice: " + isServiceRunning);
Log.e("ServiceAliveUtils", mContext.getClass().getName() + "isServiceAlice: " + isServiceRunning);
return isServiceRunning;
}
}

View File

@@ -0,0 +1,24 @@
package com.mjsheng.myappstore.utils;
import android.app.ActivityManager;
import android.content.Context;
import java.util.List;
public class SystemUtils {
public static boolean IsMainProcessName(Context cxt, int pid) {
String packageName = cxt.getPackageName();
ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
if (runningApps == null) {
return false;
}
for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
if (procInfo.pid == pid) {
return procInfo.processName.equals(packageName);
}
}
return false;
}
}