【问题标题】:Detect Mock Location is enabled or disabled in Flutter在 Flutter 中启用或禁用检测模拟位置
【发布时间】:2019-07-22 15:43:07
【问题描述】:

我的问题是我正在使用颤振平台为我的客户开发一个应用程序,我希望我开发的应用程序应该能够从 android 手机设置中检测到模拟位置状态,以便我可以检查该位置是否来自 gps提供商或模拟位置应用程序。如果启用了模拟位置,那么我的应用程序应该会抛出错误消息

【问题讨论】:

    标签: flutter


    【解决方案1】:

    Barzan 的回答很好,还有一个 Flutter 包,名字叫 trust_location,可以找到here

    您可以使用它来检查模拟位置:

    bool isMockLocation = await TrustLocation.isMockLocation;
    

    所以,我推荐使用它。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我通过在 java 中编码并在颤振项目中实现来解决它。 这是我所做的: 1) 将此添加到您在 Flutter 项目中的 Main_Activity 中。

      import android.content.Context;
      import android.content.pm.ApplicationInfo;
      import android.content.pm.PackageInfo;
      import android.content.pm.PackageManager;
      import android.os.Bundle;
      
      import io.flutter.app.FlutterActivity;
      import io.flutter.plugins.GeneratedPluginRegistrant;
      import io.flutter.plugin.common.MethodCall;
      import io.flutter.plugin.common.MethodChannel;
      import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
      import io.flutter.plugin.common.MethodChannel.Result;
      
      import android.content.ContextWrapper;
      import android.content.Intent;
      import android.content.IntentFilter;
      import android.os.BatteryManager;
      import android.os.Build.VERSION;
      import android.os.Build.VERSION_CODES;
      import android.os.Bundle;
      import android.provider.Settings;
      import android.util.Log;
      
      import java.util.List;
      
      public class MainActivity extends FlutterActivity {
          private static final String CHANNEL = "samples.flutter.io/location";
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              GeneratedPluginRegistrant.registerWith(this);
      
              new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                      new MethodCallHandler() {
                          @Override
                          public void onMethodCall(MethodCall call, Result result) {
      
                              if (call.method.equals("getLocation")) {
                                  boolean b = getMockLocation();
                                  result.success(b);
                              } else {
                                  result.notImplemented();
                              }
                          }
                      });
      
          }
      
          public static boolean isMockSettingsON(Context context) {
              // returns true if mock location enabled, false if not enabled.
              if (VERSION.SDK_INT >= VERSION_CODES.CUPCAKE) {
                  if (Settings.Secure.getString(context.getContentResolver(),
                          Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
                      return false;
                  else
                      return true;
              }
              return false;
          }
      
      
      
          public static boolean areThereMockPermissionApps(Context context) {
              int count = 0;
      
              PackageManager pm = context.getPackageManager();
              List<ApplicationInfo> packages =
                      pm.getInstalledApplications(PackageManager.GET_META_DATA);
      
              for (ApplicationInfo applicationInfo : packages) {
                  try {
                      PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName,
                              PackageManager.GET_PERMISSIONS);
      
                      // Get Permissions
                      String[] requestedPermissions = packageInfo.requestedPermissions;
      
                      if (requestedPermissions != null) {
                          for (int i = 0; i < requestedPermissions.length; i++) {
                              if (requestedPermissions[i]
                                      .equals("android.permission.ACCESS_MOCK_LOCATION")
                                      && !applicationInfo.packageName.equals(context.getPackageName())) {
                                  count++;
                              }
                          }
                      }
                  } catch (PackageManager.NameNotFoundException e) {
                      Log.e("Got exception " , e.getMessage());
                  }
              }
      
              if (count > 0)
                  return true;
              return false;
          }
      
      
          private boolean getMockLocation() {
              boolean b ;
              b= areThereMockPermissionApps(MainActivity.this);
              return b;
          }
      }
      

      2) 在你的 flutter_dart 代码中使用它,如下所示:

        static const platform = const MethodChannel('samples.flutter.io/location');
      
      
      
      bool mocklocation = false;
        Future<void> _getMockLocation() async {
          bool b;
          try {
            final bool result = await platform.invokeMethod('getLocation');
            b = result;
          } on PlatformException catch (e) {
            b = false;
          }
      
          mocklocation = b;
        }
      
      
      if (mocklocation == true) {
      
             return showDialog(
                  barrierDismissible: false,
                  context: context,
                  builder: (BuildContext context) {
                    return WillPopScope(
                      onWillPop: (){},
                                    child: AlertDialog(
                        title: Text('Location'),
                        content: Text('Your Location is fake'),
                      ),
                    );
                  });
          }
      

      3) 更多信息和示例: https://flutter.dev/docs/development/platform-integration/platform-channels

      【讨论】:

        猜你喜欢
        • 2015-05-27
        • 2022-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-18
        • 2012-06-05
        • 2018-12-18
        相关资源
        最近更新 更多