From ad8195d5648bd45bbae9b2cb780d2a180abb8a83 Mon Sep 17 00:00:00 2001 From: snickersun Date: Tue, 17 Mar 2020 12:47:02 +0800 Subject: [PATCH] =?utf8?q?1=E3=80=81=E4=BF=AE=E5=A4=8DGPS=20=E7=8A=B6?= =?utf8?q?=E6=80=81=E7=AA=97=E5=8F=A3Wakelock=20crash=E9=97=AE=E9=A2=98=20?= =?utf8?q?2=E3=80=81=E6=8A=8A=E6=89=80=E6=9C=89log=E6=96=87=E4=BB=B6?= =?utf8?q?=E6=8C=87=E5=90=91=20/sdcard/Android/ic=203=E3=80=81=E6=8A=8A?= =?utf8?q?=E6=89=80=E6=9C=89GPS=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= =?utf8?q?=E9=83=BD=E7=A7=BB=E5=88=B0=20NMEAListener=E4=B8=AD=EF=BC=8C?= =?utf8?q?=E8=A7=A3=E5=86=B3=E5=81=B6=E5=B0=94=E6=9C=89GPS=20unregister=20?= =?utf8?q?listener=20=EF=BC=8C=E4=B8=8B=E6=AC=A1=E5=BC=80=E5=90=AFGPS=20?= =?utf8?q?=E5=A4=B1=E7=81=B5=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../com/enqualcomm/support/CoreService.java | 4 ++- .../com/enqualcomm/support/CrashHandler.java | 3 +- .../com/enqualcomm/support/GPSActivity.java | 10 ++++-- .../enqualcomm/support/LocationClient.java | 31 ++++++++++--------- .../com/enqualcomm/support/NMEAListener.java | 22 ++++++++++++- .../support/utils/WakeLockUtil.java | 3 +- app/src/main/res/layout/activity_main.xml | 6 ++-- app/src/main/res/layout/activity_tool.xml | 18 +++++++---- local.properties | 4 +-- 9 files changed, 71 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/enqualcomm/support/CoreService.java b/app/src/main/java/com/enqualcomm/support/CoreService.java index 1a03a9f5..a7d77c72 100644 --- a/app/src/main/java/com/enqualcomm/support/CoreService.java +++ b/app/src/main/java/com/enqualcomm/support/CoreService.java @@ -21,6 +21,7 @@ import android.widget.Toast; import com.enqualcomm.support.bean.ICLocation; import com.enqualcomm.support.utils.DeviceInfoUtil; import com.enqualcomm.support.utils.ServiceUtil; +import com.enqualcomm.support.utils.StorageUtils; import com.ic.jni.ICJniUtils; import com.enqualcomm.support.Constants; import com.enqualcomm.support.GSensorListener; @@ -53,6 +54,7 @@ import com.enqualcomm.support.utils.SharPreferenceUtil; import com.enqualcomm.support.utils.SimSignalUtil; import com.enqualcomm.support.utils.TimeUtil; import com.enqualcomm.support.utils.Util; +import com.sina.util.dnscache.log.FileUtil; import java.lang.ref.WeakReference; import chestnut.ui.Toastc; @@ -351,7 +353,7 @@ public class CoreService extends Service { private void getDBFile() { StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(getExternalCacheDir()); + stringBuilder.append(StorageUtils.getExternalStorageLogDir()); stringBuilder.append("/contacts.db"); FileOutputUtil.copyFolder("/data/data/com.enqualcomm.support/databases", stringBuilder.toString()); } diff --git a/app/src/main/java/com/enqualcomm/support/CrashHandler.java b/app/src/main/java/com/enqualcomm/support/CrashHandler.java index 260ac33b..87517b73 100644 --- a/app/src/main/java/com/enqualcomm/support/CrashHandler.java +++ b/app/src/main/java/com/enqualcomm/support/CrashHandler.java @@ -10,6 +10,7 @@ import android.util.Log; import android.widget.Toast; import com.enqualcomm.support.R; +import com.enqualcomm.support.utils.StorageUtils; import java.io.File; import java.io.FileOutputStream; @@ -199,7 +200,7 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler { // if (Environment.getExternalStorageState().equals( // Environment.MEDIA_MOUNTED)) { // File dir = new File(mContext.getExternalCacheDir(), "crash"); - File dir=mContext.getExternalCacheDir(); + File dir= new File(StorageUtils.getExternalStorageLogDir()); String path = dir.getPath(); if (!dir.exists()) { dir.mkdirs(); diff --git a/app/src/main/java/com/enqualcomm/support/GPSActivity.java b/app/src/main/java/com/enqualcomm/support/GPSActivity.java index 57ae3dca..5c35dabd 100644 --- a/app/src/main/java/com/enqualcomm/support/GPSActivity.java +++ b/app/src/main/java/com/enqualcomm/support/GPSActivity.java @@ -147,7 +147,8 @@ public class GPSActivity extends AppCompatActivity implements location_callback // timer.schedule(tmt,0,2000); // showmessage=true; NMEAListener.set_callback(this); - screenOnWakelock = WakeLockUtil.getScreenOnWakeLock(this,GPSActivity.class.getSimpleName()); + if (screenOnWakelock==null) + screenOnWakelock = WakeLockUtil.getScreenOnWakeLock(this,GPSActivity.class.getSimpleName()); } @Override @@ -159,7 +160,12 @@ public class GPSActivity extends AppCompatActivity implements location_callback // timer = null; // } showmessage=false; - screenOnWakelock.release(); + if(screenOnWakelock!=null) { + if (screenOnWakelock.isHeld()) { + screenOnWakelock.release(); + } + screenOnWakelock=null; + } } } diff --git a/app/src/main/java/com/enqualcomm/support/LocationClient.java b/app/src/main/java/com/enqualcomm/support/LocationClient.java index f503f065..0add0098 100644 --- a/app/src/main/java/com/enqualcomm/support/LocationClient.java +++ b/app/src/main/java/com/enqualcomm/support/LocationClient.java @@ -215,23 +215,25 @@ public class LocationClient { } // public void EqcAlgorithmInterfaceMethod2() { public void startGpsLocate() { - ICLogger.i("开启GPS扫描"); - - if(!nmeaListener.isListening()) { - runtime.getLocationInstance().setLocationStat(ICLocation.LOCATION_STAT.LOCATION_OPEN); //此处为了检测是否定位时可以正确识别状态,以防一直处于GPS定位成功状态 - - //防止与工厂测试冲突 - if (!DeviceInfoUtil.isFactoryTestTopActivity(runtime.getContextProxy().context)) { - - Settings.Secure.setLocationProviderEnabled(runtime.getContextProxy().getResolver(), GPS_PROVIDER, true); + //防止与工厂测试冲突 + if (!DeviceInfoUtil.isFactoryTestTopActivity(runtime.getContextProxy().context)) { + ICLogger.i("开启GPS扫描!"); + if(Config.LOG_TOFILE) ICLogger.logToFile("GPS","开启GPS扫描!"); + if (!nmeaListener.isListening()) { + runtime.getLocationInstance().setLocationStat(ICLocation.LOCATION_STAT.LOCATION_OPEN); //此处为了检测是否定位时可以正确识别状态,以防一直处于GPS定位成功状态 Single.just(null).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1() { public void call(Object param1Object) { nmeaListener.startListen(); } }); + + } else { + ICLogger.i("GPS is still listening, skip open it!"); + if (Config.LOG_TOFILE) ICLogger.logToFile("GPS", "GPS is still listening, skip open it!"); } }else{ - ICLogger.i("GPS is still listening, skip open it!"); + ICLogger.i("工厂模式,skip 开启GPS扫描!"); + if(Config.LOG_TOFILE) ICLogger.logToFile("GPS","工厂模式,skip 开启GPS扫描!"); } } @@ -425,17 +427,18 @@ public class LocationClient { // Deliver.is_active_location = false; // getLocationWakeLock(); // } - ICLogger.i("stopGpsLocate"); - //防止与工厂测试冲突 if (!DeviceInfoUtil.isFactoryTestTopActivity(runtime.getContextProxy().context)) { - - Settings.Secure.setLocationProviderEnabled(runtime.getContextProxy().getResolver(), GPS_PROVIDER, false); + ICLogger.i("stopGpsLocate"); + if(Config.LOG_TOFILE) ICLogger.logToFile("GPS","stopGpsLocate"); Single.just(null).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1() { public void call(Object param1Object) { nmeaListener.stopListen(); } }); + }else{ + ICLogger.i("工厂模式 skip stopGpsLocate"); + if(Config.LOG_TOFILE) ICLogger.logToFile("GPS","工厂模式 skip stopGpsLocate"); } } diff --git a/app/src/main/java/com/enqualcomm/support/NMEAListener.java b/app/src/main/java/com/enqualcomm/support/NMEAListener.java index 4adfe4e3..1f4480c6 100644 --- a/app/src/main/java/com/enqualcomm/support/NMEAListener.java +++ b/app/src/main/java/com/enqualcomm/support/NMEAListener.java @@ -12,6 +12,8 @@ import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.provider.Settings; + import com.enqualcomm.support.bean.ICLocation; import com.enqualcomm.support.bean.Runtime; import com.enqualcomm.support.utils.FileOutputUtil; @@ -23,8 +25,14 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import rx.Single; +import rx.android.schedulers.AndroidSchedulers; +import rx.functions.Action1; + +import static android.location.LocationManager.GPS_PROVIDER; + public class NMEAListener implements NmeaListener, LocationListener { - private boolean listening=false; + private static boolean listening=false; public boolean isListening(){ return listening; @@ -507,6 +515,9 @@ public class NMEAListener implements NmeaListener, LocationListener { public boolean startListen() { if (this.listening == false) { + Settings.Secure.setLocationProviderEnabled(runtime.getContextProxy().getResolver(), GPS_PROVIDER, true); + + Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);// 设置为最大精度 criteria.setAltitudeRequired(false);//不要求海拔信息 @@ -516,8 +527,13 @@ public class NMEAListener implements NmeaListener, LocationListener { String provider = locationManager.getBestProvider(criteria, true); + ICLogger.i("GPS StartListen with Provider " + provider); + + if( Config.LOG_TOFILE) ICLogger.logToFile("GPS","GPS StartListen with Provider " + provider); + this.locationManager.requestLocationUpdates(provider, 0L, 0.0F, this); + this.listening = true; noSignalCount = 0; longitude = 0; @@ -532,6 +548,7 @@ public class NMEAListener implements NmeaListener, LocationListener { return true;//gpsHotStart(); }else{ ICLogger.i(" location listening, skip !"); + if( Config.LOG_TOFILE)ICLogger.logToFile("GPS"," location listening, skip !"); return true; } @@ -543,6 +560,9 @@ public class NMEAListener implements NmeaListener, LocationListener { this.locationManager.removeUpdates(this); this.listening = false; ICLogger.i("stopListen 定位 "); + if( Config.LOG_TOFILE)ICLogger.logToFile("GPS","stopListen 定位 "); + Settings.Secure.setLocationProviderEnabled(runtime.getContextProxy().getResolver(), GPS_PROVIDER, false); + } public enum GPS_CURRENT_STATUS { diff --git a/app/src/main/java/com/enqualcomm/support/utils/WakeLockUtil.java b/app/src/main/java/com/enqualcomm/support/utils/WakeLockUtil.java index 9db26546..374b2274 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/WakeLockUtil.java +++ b/app/src/main/java/com/enqualcomm/support/utils/WakeLockUtil.java @@ -9,7 +9,8 @@ import java.util.Objects; public class WakeLockUtil { public static PowerManager.WakeLock getScreenOnWakeLock(Context context, String tag) { PowerManager.WakeLock wakeLock = ((PowerManager)(context.getSystemService(Context.POWER_SERVICE))).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, tag); - wakeLock.acquire(900000L); +// wakeLock.acquire(900000L); + wakeLock.acquire(); return wakeLock; } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 32a286a2..070d23e0 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -35,14 +35,16 @@ android:layout_width="80dp" android:layout_height="wrap_content" android:layout_weight="1" - android:text="重启服务" /> + android:text="重启服务" + android:textSize="18sp" />