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

CA *_*ARG 12 flutter

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

bar*_*our 5

我遇到了同样的问题,我通过在 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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'),
                ),
              );
            });
    }
Run Code Online (Sandbox Code Playgroud)

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