标签:android locationmanager gps android-pendingintent
在我正在处理的应用程序中,其中一个功能是在用户到达之前设置的位置时通知用户.
以下代码位于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 – 这意味着我的应用程序不再通知用户.但是我希望在他离开该位置时通知用户,直到他取消,或者即使他们关闭应用程序也已经通知.
我错过了什么?
解决方法:
如果用户关闭了您的活动,您应该取消注册您的位置监听器.
听起来您需要移动部分应用程序(监视位置并警告用户的位)到后台服务,以便即使在用户关闭应用程序后它也可以继续运行.
标签:android,locationmanager,gps,android-pendingintent 来源: https://codeday.me/bug/20190901/1785426.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。