我正在开发一个带有提醒功能的ToDo应用程序(根据时间和地点),让用户选择是否希望在进入或离开某个位置时进行提醒。 如何实现这一功能?
我知道KEY_PROXIMITY_ENTERING,但我不知道如何使用它。 请帮忙... 先感谢了。
我正在开发一个带有提醒功能的ToDo应用程序(根据时间和地点),让用户选择是否希望在进入或离开某个位置时进行提醒。 如何实现这一功能?
我知道KEY_PROXIMITY_ENTERING,但我不知道如何使用它。 请帮忙... 先感谢了。
KEY_PROXIMITY_ENTERING通常用于确定设备是进入还是退出。
您应该首先向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发送的广播:
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();
}
}
}
然后在您的清单文件中注册接收器。
<receiver android:name="yourpackage.YourReceiver " >
<intent-filter>
<action android:name="ACTION_PROXIMITY_ALERT" />
</intent-filter>
</receiver>