gpt4 book ai didi

java - Android:addProximityAlert LocationManager 方法不发送广播

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:00 30 4
gpt4 key购买 nike

我正在开发一个 Android 项目,我正在使用 AddProximityAlert 方法,因为您已经知道此方法让您可以为位置(纬度、经度)和给定半径给定的位置设置接近警报,并通知如果你离它如此之近。

三天前我一直在研究这个问题,但我一次又一次地遇到同样的问题..

in bref: 这是我的简单代码。

#MainActivity.java

package com.example.proximityalert;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity implements LocationListener {

LocationManager lm;

//Defining Latitude & Longitude
double lat=37.422006 ,long1=-122.084095;
//Defining Radius
float radius=1000;

//Intent Action
String ACTION_FILTER = "com.example.proximityalert";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//i'm registering my Receiver First
registerReceiver(new ProximityReciever(), new IntentFilter(ACTION_FILTER));

//i'm calling ther service Location Manager
lm=(LocationManager) getSystemService(LOCATION_SERVICE);

//for debugging...
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);

//Setting up My Broadcast Intent
Intent i= new Intent(ACTION_FILTER);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);

//setting up proximituMethod
lm.addProximityAlert(lat, long1, radius, -1, pi);

}

@Override
//just For debugging to See the distance between my actual position and the aproximit point
public void onLocationChanged(Location newLocation) {

Location old = new Location("OLD");
old.setLatitude(lat);
old.setLongitude(long1);

double distance = newLocation.distanceTo(old);

Log.i("MyTag", "Distance: " + distance);
}

@Override
public void onProviderDisabled(String arg0) {}

@Override
public void onProviderEnabled(String arg0) {}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

}

#ProximityReceiver.java

package com.example.proximityalert;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;


public class ProximityReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

// Key for determining whether user is leaving or entering
String key = LocationManager.KEY_PROXIMITY_ENTERING;

//Gives whether the user is entering or leaving in boolean form
boolean state = intent.getBooleanExtra(key, false);

if(state){
// Call the Notification Service or anything else that you would like to do here
Log.i("MyTag", "Welcome to my Area");
Toast.makeText(context, "Welcome to my Area", Toast.LENGTH_SHORT).show();
}else{
//Other custom Notification
Log.i("MyTag", "Thank you for visiting my Area,come back again !!");
Toast.makeText(context, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_SHORT).show();
}
}
}

*#问题是当我运行程序时,BroadcastReceiver(ProximityReciever) 永远不会被系统调用,即使我非常接近接近点,即使调试器向我显示两个位置之间的距离是 < 1000 米 :/

最佳答案

我刚刚弄清楚关于这个主题的一些事情以及为什么 addProximityAlert sames 不起作用,我与你分享这个是因为我注意到之前有人问过同样的问题但他们没有得到任何答案!

答案就在我面前,但我没有注意它,所以当我阅读 Android 官方文档(here)时,我看到了这句话“Due to the approximate位置估计的性质,如果设备短暂地通过给定区域,则可能不会触发任何 Intent

这是什么意思?这意味着当你在 AVD 上测试你的应用程序并且你从 DDMS 向 AVD 发送 gps 坐标(纬度,经度)时,它真的很难 模拟 gps 的真实方面,(因为首先你选择一些点作为你的 proximPoint 然后你选择离 proximPoint 很远的 anchor 以查看它是否工作)并且那不是它与真实的事情设备。

因此解决方案是在真实设备上测试您的应用程序或使用 DDMS 尝试非常缓慢地更改坐标,直到您位于所需区域。

关于java - Android:addProximityAlert LocationManager 方法不发送广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23034298/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com