|
@@ -1,28 +1,41 @@
|
|
|
package com.atmob.map_amap_android.util;
|
|
package com.atmob.map_amap_android.util;
|
|
|
|
|
|
|
|
|
|
+import android.Manifest;
|
|
|
import android.annotation.SuppressLint;
|
|
import android.annotation.SuppressLint;
|
|
|
import android.app.Application;
|
|
import android.app.Application;
|
|
|
|
|
+import android.app.Notification;
|
|
|
|
|
+import android.app.NotificationChannel;
|
|
|
|
|
+import android.app.NotificationManager;
|
|
|
|
|
+import android.app.PendingIntent;
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
-import android.util.Log;
|
|
|
|
|
|
|
+import android.content.Intent;
|
|
|
|
|
+import android.os.Build;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
+
|
|
|
import com.amap.api.location.AMapLocation;
|
|
import com.amap.api.location.AMapLocation;
|
|
|
import com.amap.api.location.AMapLocationClient;
|
|
import com.amap.api.location.AMapLocationClient;
|
|
|
import com.amap.api.location.AMapLocationClientOption;
|
|
import com.amap.api.location.AMapLocationClientOption;
|
|
|
|
|
+import com.atmob.map_amap_android.R;
|
|
|
|
|
+
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
|
|
public class AMapHelper {
|
|
public class AMapHelper {
|
|
|
private static final String TAG = "AMapHelper";
|
|
private static final String TAG = "AMapHelper";
|
|
|
- public static final String KEY_LAST_TRACK_ID = "key_amap_last_track_id";
|
|
|
|
|
- private static final int AMAP_NOTIFICATION_ID = 340;
|
|
|
|
|
- private static final String AMAP_NOTIFICATION_CHANNEL_ID = "amap_notif_channel_id";
|
|
|
|
|
|
|
+ public static final String KEY_LAST_TRACK_ID = "amap_last_track_id";
|
|
|
|
|
+ private static final int AMAP_NOTIFICATION_ID = 350;
|
|
|
|
|
+ private static final String AMAP_NOTIFICATION_CHANNEL_ID = "amap_notification_channel_id";
|
|
|
private static final String AMAP_NOTIFICATION_CHANNEL_NAME = "Location Tracking";
|
|
private static final String AMAP_NOTIFICATION_CHANNEL_NAME = "Location Tracking";
|
|
|
private static final AtomicBoolean isLocationInit = new AtomicBoolean(false);
|
|
private static final AtomicBoolean isLocationInit = new AtomicBoolean(false);
|
|
|
private static final ArrayList<OnLocationListener> locationListeners = new ArrayList<>();
|
|
private static final ArrayList<OnLocationListener> locationListeners = new ArrayList<>();
|
|
|
private static AMapLocation lastLocation;
|
|
private static AMapLocation lastLocation;
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
private static AMapLocationClient aMapLocationClient;
|
|
private static AMapLocationClient aMapLocationClient;
|
|
|
|
|
+ private static final String[] LOCATION_PERMISSION = new String[]{
|
|
|
|
|
+ Manifest.permission.ACCESS_FINE_LOCATION,
|
|
|
|
|
+ Manifest.permission.ACCESS_COARSE_LOCATION
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
public static void init(Context context) {
|
|
public static void init(Context context) {
|
|
|
if (ProcessUtil.isMainProcess(context)) {
|
|
if (ProcessUtil.isMainProcess(context)) {
|
|
@@ -33,7 +46,7 @@ public class AMapHelper {
|
|
|
private static void initLocation(Context context) {
|
|
private static void initLocation(Context context) {
|
|
|
if (isLocationInit.compareAndSet(false, true)) {
|
|
if (isLocationInit.compareAndSet(false, true)) {
|
|
|
try {
|
|
try {
|
|
|
- Log.d(TAG, "amap ... initLocation...");
|
|
|
|
|
|
|
+ LogUtil.d(TAG, "amap ... initLocation...");
|
|
|
AMapLocationClient.updatePrivacyShow(context, true, true);
|
|
AMapLocationClient.updatePrivacyShow(context, true, true);
|
|
|
AMapLocationClient.updatePrivacyAgree(context, true);
|
|
AMapLocationClient.updatePrivacyAgree(context, true);
|
|
|
aMapLocationClient = new AMapLocationClient(context);
|
|
aMapLocationClient = new AMapLocationClient(context);
|
|
@@ -42,25 +55,62 @@ public class AMapHelper {
|
|
|
if (aMapLocation != null) {
|
|
if (aMapLocation != null) {
|
|
|
if (aMapLocation.getErrorCode() == 0) {
|
|
if (aMapLocation.getErrorCode() == 0) {
|
|
|
lastLocation = aMapLocation;
|
|
lastLocation = aMapLocation;
|
|
|
- Log.d(TAG, "onLocationChanged: " + aMapLocation);
|
|
|
|
|
|
|
+ LogUtil.d(TAG, "onLocationChanged: " + aMapLocation);
|
|
|
for (OnLocationListener locationListener : locationListeners) {
|
|
for (OnLocationListener locationListener : locationListeners) {
|
|
|
locationListener.onLocationChanged(aMapLocation);
|
|
locationListener.onLocationChanged(aMapLocation);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
-// if (PermissionUtil.hasPermission(LocationUtil.getLocationPermissions())) {
|
|
|
|
|
-// aMapLocationClient.startLocation();
|
|
|
|
|
-// aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(application));
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+ if (PermissionUtil.hasPermission(context, LOCATION_PERMISSION)) {
|
|
|
|
|
+ aMapLocationClient.startLocation();
|
|
|
|
|
+// aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(context));
|
|
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
isLocationInit.set(false);
|
|
isLocationInit.set(false);
|
|
|
- Log.e(TAG, "initLocation failed.", e);
|
|
|
|
|
|
|
+ LogUtil.e(TAG, "initLocation failed.", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static void startLocation(Context context) {
|
|
|
|
|
+ if (isLocationInit.get()) {
|
|
|
|
|
+ aMapLocationClient.startLocation();
|
|
|
|
|
+// aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(context));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ initLocation(context);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
+// private static Notification createLocationNotification(Application application) {
|
|
|
|
|
+// NotificationManager notificationManager = (NotificationManager) application.getSystemService(Application.NOTIFICATION_SERVICE);
|
|
|
|
|
+// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
+// NotificationChannel notificationChannel = new NotificationChannel(AMAP_NOTIFICATION_CHANNEL_ID,
|
|
|
|
|
+// AMAP_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
|
+// notificationManager.createNotificationChannel(notificationChannel);
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// Intent splashIntent = new Intent(application, .class);
|
|
|
|
|
+// splashIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
+//
|
|
|
|
|
+// PendingIntent contentIntent = PendingIntent.getActivity(
|
|
|
|
|
+// application,
|
|
|
|
|
+// 12344,
|
|
|
|
|
+// splashIntent,
|
|
|
|
|
+// PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
|
|
|
|
+// );
|
|
|
|
|
+//
|
|
|
|
|
+// return new NotificationCompat.Builder(application, AMAP_NOTIFICATION_CHANNEL_ID)
|
|
|
|
|
+// .setContentTitle(application.getString(R.string.location_notification_title))
|
|
|
|
|
+// .setContentText(application.getString(R.string.location_notification_content))
|
|
|
|
|
+// .setSmallIcon(R.drawable.icon_location_notification_small)
|
|
|
|
|
+// .setContentIntent(contentIntent)
|
|
|
|
|
+// .setOngoing(true)
|
|
|
|
|
+// .setSilent(true)
|
|
|
|
|
+// .setAutoCancel(false)
|
|
|
|
|
+// .build();
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
public static AMapLocation getLastLocation() {
|
|
public static AMapLocation getLastLocation() {
|
|
|
return lastLocation;
|
|
return lastLocation;
|