Android 接近警报不会被触发

发布于 2024-09-26 05:41:42 字数 1517 浏览 8 评论 0原文

这是我的第一个问题:

我正在尝试配置将从数据库和网络服务提供商提供的接近警报;但我在配置简单的接近警报进行测试时遇到问题。我设法创建警报,但它从未被触发,我现在只是在模拟器中尝试,不知道是否需要一些额外的代码来触发警报。

我在某处读到过,禁用 GPS 提供商,以便可以使用网络提供商来触发模拟器上的警报。

我的代码如下所示:

接近意图声明

private String proximityIntentAction = new String("gpsdelivery.gpship.getLocation.GPS_PROXIMITY_ALERT");    

在 onStart() 内部设置警报参数的地方

IntentFilter intentFilter = new IntentFilter(proximityIntentAction);
registerReceiver(new ProximityAlert(), intentFilter);
setProximityAlert(45.150344, 9.999815, -1);        

接近警报函数,在其中创建警报的地方

private void setProximityAlert(double lat, double lon, int requestCode)
{
    // 100 meter radius
    float radius = 100f;
    // Expiration is 10 Minutes
    long expiration = 600000;
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Intent intent = new Intent(proximityIntentAction);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);

}

最后是带有广播接收器的接近警报类

public class ProximityAlert extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            Log.v("SomeTag","Proximity alert received");

        }

    }       

请让我知道我缺少什么或什么我做错了吗?提前致谢,任何源代码将不胜感激。

this is my very first question:

I'm trying to configure proximity alerts that will be feed from a Database and webservice provider; but I have a problem configuring a simple proximity alert for testing. I manage to create the alert but it never gets fired, I'm only trying in the emulator for now and don´t know if I need some extra code to trigger the alerts.

I've read somewhere that the GPS provider to disabled so the network provider can be used in order to trigger the alerts on the emulator.

My code looks like this:

Proximity intent declaration

private String proximityIntentAction = new String("gpsdelivery.gpship.getLocation.GPS_PROXIMITY_ALERT");    

Inside onStart() where the parameters of the alerts are set

IntentFilter intentFilter = new IntentFilter(proximityIntentAction);
registerReceiver(new ProximityAlert(), intentFilter);
setProximityAlert(45.150344, 9.999815, -1);        

Proximity alert function where the alerts get created

private void setProximityAlert(double lat, double lon, int requestCode)
{
    // 100 meter radius
    float radius = 100f;
    // Expiration is 10 Minutes
    long expiration = 600000;
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Intent intent = new Intent(proximityIntentAction);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);

}

And finally my proximity alert class with the Broadcast receiver

public class ProximityAlert extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            Log.v("SomeTag","Proximity alert received");

        }

    }       

Please let me know what I'm missing or what am I doing wrong. Thanks in advance, any source code would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-10-03 05:41:42

好吧,我相信您的 setProximityAlert(45.150344, 9.999815, -1); 不正确。具体来说,你的-1。它不包含 locManager.addProximityAlert(lat, lon, radius,expiration,pendingIntent); 中所需的信息。

Well, I believe your setProximityAlert(45.150344, 9.999815, -1); is incorrect. Specifically, your -1. It doesn't contain the information needed in the locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文