Android:如何将接近警报设置为仅在退出时或仅在进入位置时才触发 [英] Android: how to set a proximity alert to fire only when exiting or only when entering the location

查看:99
本文介绍了Android:如何将接近警报设置为仅在退出时或仅在进入位置时才触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发带有提醒(按时间和位置)的ToDo应用,问题是我给用户一个选项,供他选择是否要按地点提醒在输入时发出警报位置或他退出位置的时间. 我该怎么办??

I am developing a ToDo app with reminders(by time and by location) and the thing is I give the user the option to choose if he wants the reminder by location to alert when he is entering the location or when he exits the location. how can i do that??

我了解 KEY_PROXIMITY_ENTERING ,但是我不知道如何使用它 请帮忙... 提前谢谢

I know about the KEY_PROXIMITY_ENTERING but I dont know how to use it Please help... thanx in advance

推荐答案

KEY_PROXIMITY_ENTERING通常用于确定设备是进入还是退出.

The KEY_PROXIMITY_ENTERING is usually used to determine whether the device is entering or exiting.

您应该先注册到LocationManager

You should first register to the LocationManager

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Intent intent = new Intent(Constants.ACTION_PROXIMITY_ALERT);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);

locationManager.addProximityAlert(location.getLatitude(),
    location.getLongitude(), location.getRadius(), -1, pendingIntent);

当检测到进入或退出警报区域时,将使用PendingIntent生成要触发的Intent. 您应该定义一个广播接收器以接收从LocationManager发送的广播:

The PendingIntent will be used to generate an Intent to fire when entry to or exit from the alert region is detected. You should define a broadcast receiver to receive the broadcast sent from LocationManager:

public class YourReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        final String key = LocationManager.KEY_PROXIMITY_ENTERING;
        final Boolean entering = intent.getBooleanExtra(key, false);

        if (entering) {
            Toast.makeText(context, "entering", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "exiting", Toast.LENGTH_SHORT).show();
        }
    }
}

然后在清单中注册接收者.

Then register the receiver in your manifest.

<receiver android:name="yourpackage.YourReceiver " >
    <intent-filter>
        <action android:name="ACTION_PROXIMITY_ALERT" />
    </intent-filter>
</receiver>

这篇关于Android:如何将接近警报设置为仅在退出时或仅在进入位置时才触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆