feat(gpsrelaysentinel): 新增MainService服务用于接收GPS定位消息
- 添加MainService服务类,监听GPS定位更新(1秒间隔,1米距离) - 在AndroidManifest.xml注册MainService服务 - 添加定位权限ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION - 使用LogUtils替代android.util.Log进行日志记录 - TAG属性改为public static final
This commit is contained in:
@@ -3,6 +3,9 @@
|
|||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="cc.winboll.studio.gpsrelaysentinel">
|
package="cc.winboll.studio.gpsrelaysentinel">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
@@ -32,6 +35,10 @@
|
|||||||
|
|
||||||
<activity android:name=".GlobalApplication$CrashActivity"/>
|
<activity android:name=".GlobalApplication$CrashActivity"/>
|
||||||
|
|
||||||
|
<service android:name=".MainService"
|
||||||
|
android:enabled="true"
|
||||||
|
android:exported="false" />
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user