java - 当设备在标记的 10 米内时调用该方法
问题描述
在我的应用程序中,我获得了设备的坐标。现在我正在尝试使设备在 10 米半径内时调用该方法。找到 addProximityAlert 方法。但是你需要传递我需要的点的坐标,但我不知道如何链接我需要的标记和设备的位置。我已经创建了一个广播接收器。请提供建议或文章。
public void getlocate() {
if (flag) {
Intent intent = new Intent("com.runline.workertrack");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, -1, intent, 0);
try {
enable = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
}
if (enable) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5, listener);
manager.addProximityAlert(0,0 , 100, -1, pendingIntent);
}
}
}
public boolean checkLocationPermission()
{
int loc1 = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listper = new ArrayList<>();
if(loc1 != PackageManager.PERMISSION_GRANTED)
listper.add(Manifest.permission.ACCESS_FINE_LOCATION);
if(!listper.isEmpty()) {
ActivityCompat.requestPermissions(this, listper.toArray(new String[listper.size()]), 1);
}
return true;
}
class Locate implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
manager.removeUpdates(listener);
shirota = "" + location.getLatitude();
dolgota = "" + location.getLongitude();
shir.setText(shirota);
dolg.setText(dolgota);
final Map<String, Object> hashMap = new HashMap<>();
hashMap.put("shirota", shirota);
hashMap.put("dolgota", dolgota);
hashMap.put("id", "1");
reference.setValue(hashMap);
IntentFilter filter = new IntentFilter("com.runline.workertrack");
registerReceiver(new ProximityIntentReceiver(), filter);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
public class ProximityIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if(entering){
Toast.makeText(getApplicationContext(), "You are here!", Toast.LENGTH_LONG).show();
}
}
}
解决方案
推荐阅读
- mysql - UNIX_TIMESTAMP MYSQL,无法获得正确的查询
- nightwatch.js - 在 Windows PC 上获取“错误:未定义源文件夹。检查配置”
- react-native - React Native Admob 插件 - 使用最新的 React Native 构建失败
- sql - 我可以在我的数据仓库设计中使用 excel 事务文件作为事实表吗
- python - python jupyter notebook中使用drawow的动态绘图
- c++ - 尝试导入 QuickControls 时出错
- php - IIS 服务器 (PHP)$_GET 或 $_REQUEST 显示空 URL 字符串查询
- java - 等待通知传递 NULL 值 Java
- spring-cloud - 使用 Spring Cloud Gateway 的简单网关 - ProxyExchange
- python - 如何多次重命名文件(Python)