flutter - 在 Flutter 中启用或禁用检测模拟位置

标签 flutter

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

最佳答案

我有同样的问题,我通过在 java 中编码并在 flutter 项目中实现来修复它。 这是我所做的: 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

关于flutter - 在 Flutter 中启用或禁用检测模拟位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54930545/

相关文章:

listview - 如何为选定的 ListItem onTap 着色

flutter - WillPopScope:使Android后退按钮弹出实际 View

web-services - POST API不起作用,但在 flutter 打中的 postman 上有效

dart - Flutter TextField 自动完成覆盖

flutter - Flutter 中居中对齐前导图标

android - flutter 和 VSCode : Gradle Build Error

android - 按照教程创建flutter app失败

使用多个屏幕通过 Provider 进行 Flutter 状态管理

android - 是否可以使用 navigator.pop 刷新或重新加载页面...比如第 1(nav.push=>2)页面到第 2 页,然后从第 2(nav.pop,值)返回到第 1 页?

string - 在 dart 中转义字符串中的单引号和双引号