Przeglądaj źródła

[new]增加定位通知栏显示

zk 8 miesięcy temu
rodzic
commit
7b4874c1d5

+ 1 - 1
android/app/src/main/AndroidManifest.xml

@@ -2,7 +2,7 @@
 
     <application
         android:allowBackup="false"
-        android:label="location"
+        android:label="@string/app_name"
         android:name="${applicationName}"
         android:icon="@mipmap/ic_launcher">
 

+ 3 - 0
android/app/src/main/res/values/strings.xml

@@ -0,0 +1,3 @@
+<resources>
+    <string name="app_name">手机实时定位</string>
+</resources>

+ 1 - 1
assets/string/base/string.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-    <string name="app_name">手机定位追迹</string>
+    <string name="app_name">手机实时定位</string>
     <string name="main_tab_home">首页</string>
     <string name="main_tab_camera">扫一扫</string>
     <string name="main_tab_mine">我的</string>

+ 3 - 2
lib/resource/string.gen.dart

@@ -2,7 +2,8 @@ import 'package:get/get.dart';
 
 class StringName {
   StringName._();
-  static final String appName = 'app_name'.tr; // 手机定位追迹
+
+  static final String appName = 'app_name'.tr; // 手机实时定位
   static final String mainTabHome = 'main_tab_home'.tr; // 首页
   static final String mainTabCamera = 'main_tab_camera'.tr; // 扫一扫
   static final String mainTabMine = 'main_tab_mine'.tr; // 我的
@@ -127,7 +128,7 @@ class StringMultiSource {
   StringMultiSource._();
   static const Map<String, Map<String, String>> values = {
     'zh_CN': {
-      'app_name': '手机定位追迹',
+      'app_name': '手机实时定位',
       'main_tab_home': '首页',
       'main_tab_camera': '扫一扫',
       'main_tab_mine': '我的',

+ 1 - 0
plugins/map_amap_android/android/build.gradle

@@ -63,6 +63,7 @@ android {
 
         //AndroidX
         compileOnly "androidx.annotation:annotation:1.1.0"
+        implementation "androidx.core:core:1.9.0"
 
 
         //constraintlayout

+ 36 - 30
plugins/map_amap_android/android/src/main/java/com/atmob/map_amap_android/util/AMapHelper.java

@@ -12,6 +12,7 @@ import android.content.Intent;
 import android.os.Build;
 
 import androidx.annotation.NonNull;
+import androidx.core.app.NotificationCompat;
 
 import com.amap.api.location.AMapLocation;
 import com.amap.api.location.AMapLocationClient;
@@ -64,7 +65,7 @@ public class AMapHelper {
                 });
                 if (PermissionUtil.hasPermission(context, LOCATION_PERMISSION)) {
                     aMapLocationClient.startLocation();
-//                    aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(context));
+                    aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(context));
                 }
             } catch (Exception e) {
                 isLocationInit.set(false);
@@ -76,41 +77,46 @@ public class AMapHelper {
     public static void startLocation(Context context) {
         if (isLocationInit.get()) {
             aMapLocationClient.startLocation();
-//            aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(context));
+            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();
-//    }
+    private static Notification createLocationNotification(Context context) {
+        LogUtil.d(TAG, "amap ... createLocationNotification...");
+        NotificationManager notificationManager = (NotificationManager) context.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();
+        splashIntent.setAction(Intent.ACTION_MAIN);
+        splashIntent.addCategory(Intent.CATEGORY_LAUNCHER);
+        splashIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+        PendingIntent contentIntent = PendingIntent.getActivity(
+                context,
+                12345,
+                splashIntent,
+                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
+        );
+
+        String appName = SystemUtil.getAppName(context);
+
+        return new NotificationCompat.Builder(context, AMAP_NOTIFICATION_CHANNEL_ID)
+                .setContentTitle(context.getString(R.string.location_notification_title, appName))
+                .setContentText(context.getString(R.string.location_notification_content, appName))
+                .setSmallIcon(R.drawable.icon_location_notification_small)
+                .setContentIntent(contentIntent)
+                .setOngoing(true)
+                .setSilent(true)
+                .setAutoCancel(false)
+                .build();
+    }
 
     public static AMapLocation getLastLocation() {
         return lastLocation;

+ 29 - 0
plugins/map_amap_android/android/src/main/java/com/atmob/map_amap_android/util/SystemUtil.java

@@ -0,0 +1,29 @@
+package com.atmob.map_amap_android.util;
+
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+
+public class SystemUtil {
+    private SystemUtil(){}
+
+
+
+    // 获取应用名称
+    public static String getAppName(Context context) {
+        PackageManager packageManager = context.getPackageManager();
+        try {
+            ApplicationInfo appInfo = packageManager.getApplicationInfo(
+                    context.getPackageName(),
+                    PackageManager.GET_META_DATA
+            );
+            // 使用 loadLabel 获取本地化名称
+            return packageManager.getApplicationLabel(appInfo).toString();
+        } catch (PackageManager.NameNotFoundException e) {
+            e.printStackTrace();
+            return "";
+        }
+    }
+
+
+}

BIN
plugins/map_amap_android/android/src/main/res/drawable-xxhdpi/icon_location_notification_small.webp


+ 5 - 0
plugins/map_amap_android/android/src/main/res/values/strings.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="location_notification_title">%s正在守护您的安全</string>
+    <string name="location_notification_content">请勿关闭%s,请保持应用后台运行,否则会导致位置信息丢失</string>
+</resources>