gpt4 book ai didi

android - 当用户到达某个位置时通知他们

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

在我正在开发的应用程序中,其中一项功能是在用户到达他们之前设置的位置时通知他们。

下面的代码在 Activity 的 addProximityAlert 中:

final Intent intent = new Intent(PROX_ALERT_INTENT);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(
InfoActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.addProximityAlert(18.7726271, 98.9738381, 5000, -1,
pendingIntent);
this.locationReminderReceiver = new LocationReminderReceiver();
final IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
this.registerReceiver(this.locationReminderReceiver, filter);

@Override
protected void onPause() {
super.onPause();
if (this.locationReminderReceiver != null) {
Log.i("unregisterReceiver", "unregisterReceiver");
this.unregisterReceiver(this.locationReminderReceiver);
}
}

这是接收器:

public class LocationReminderReceiver 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, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show();
Log.i("LocationReminderReceiver", "entering");
} else {
Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show();
Log.i("LocationReminderReceiver", "exiting");
}
}
}

它工作正常,但我每次销毁 Activity 时都需要调用 unregisterReceiver - 这意味着我的应用程序不再通知用户。但是我想在用户靠近该位置时通知他,直到他取消,或者即使他们关闭应用程序也已经收到通知。

我错过了什么?

最佳答案

如果用户关闭了您的 Activity ,您确实应该注销您的位置监听器。

听起来您需要将应用程序的一部分(监控位置和提醒用户的部分)移动到后台服务,以便即使在用户关闭应用程序后它也可以继续运行。

关于android - 当用户到达某个位置时通知他们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638668/

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