From 58a93a6746d322aa4703b657c4d3bc3edcdb80fa Mon Sep 17 00:00:00 2001 From: LaizyBoy Date: Thu, 7 May 2026 01:50:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(gpsrelaysentinel):=20=E6=96=B0=E5=A2=9EMai?= =?UTF-8?q?nService=E6=9C=8D=E5=8A=A1=E7=94=A8=E4=BA=8E=E6=8E=A5=E6=94=B6G?= =?UTF-8?q?PS=E5=AE=9A=E4=BD=8D=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加MainService服务类,监听GPS定位更新(1秒间隔,1米距离) - 在AndroidManifest.xml注册MainService服务 - 添加定位权限ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION - 使用LogUtils替代android.util.Log进行日志记录 - TAG属性改为public static final --- gpsrelaysentinel/src/main/AndroidManifest.xml | 7 ++ .../studio/gpsrelaysentinel/MainService.java | 88 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java diff --git a/gpsrelaysentinel/src/main/AndroidManifest.xml b/gpsrelaysentinel/src/main/AndroidManifest.xml index 59a3bf0..2c92a4a 100644 --- a/gpsrelaysentinel/src/main/AndroidManifest.xml +++ b/gpsrelaysentinel/src/main/AndroidManifest.xml @@ -3,6 +3,9 @@ xmlns:android="http://schemas.android.com/apk/res/android" package="cc.winboll.studio.gpsrelaysentinel"> + + + + + diff --git a/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java b/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java new file mode 100644 index 0000000..1bad227 --- /dev/null +++ b/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java @@ -0,0 +1,88 @@ +package cc.winboll.studio.gpsrelaysentinel; + +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; +import android.os.IBinder; +import cc.winboll.studio.libappbase.LogUtils; + +public class MainService extends Service { + + public static final String TAG = "MainService"; + private LocationManager mLocationManager; + private LocationListener mLocationListener; + + @Override + public void onCreate() { + super.onCreate(); + LogUtils.d(TAG, "Service onCreate"); + initLocation(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + LogUtils.d(TAG, "Service onStartCommand"); + return START_STICKY; + } + + private void initLocation() { + mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + mLocationListener = new LocationListener() { + @Override + public void onLocationChanged(Location location) { + LogUtils.d(TAG, "Location changed: " + location.getLatitude() + ", " + location.getLongitude()); + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + LogUtils.d(TAG, "Status changed: " + provider + ", status: " + status); + } + + @Override + public void onProviderEnabled(String provider) { + LogUtils.d(TAG, "Provider enabled: " + provider); + } + + @Override + public void onProviderDisabled(String provider) { + LogUtils.d(TAG, "Provider disabled: " + provider); + } + }; + + try { + if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + mLocationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + 1000, + 1, + mLocationListener + ); + LogUtils.d(TAG, "GPS location updates requested"); + } + } catch (SecurityException e) { + LogUtils.e(TAG, "Permission denied: " + e.getMessage()); + } + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (mLocationManager != null && mLocationListener != null) { + try { + mLocationManager.removeUpdates(mLocationListener); + } catch (SecurityException e) { + LogUtils.e(TAG, "Permission denied when removing updates: " + e.getMessage()); + } + } + LogUtils.d(TAG, "Service onDestroy"); + } +}