我的问题是我正在使用Flutter平台为我的客户端开发应用程序,我希望我开发的应用程序应该能够从android手机设置中检测模拟位置状态,以便我可以检查该位置是否来自gps提供商或模拟位置应用。如果启用了模拟位置,则我的应用应该抛出错误消息
答案 0 :(得分:0)
我遇到了同样的问题,我通过用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
答案 1 :(得分:0)
Barzan的回答非常好,还有一个名为trust_location的Flutter软件包,您可以找到here。
您可以按以下方式使用它来检查模拟位置:
bool isMockLocation = await TrustLocation.isMockLocation;
所以,我建议使用它。