通知用户,当他们到达的地点 [英] Notify users when they reach a location

查看:165
本文介绍了通知用户,当他们到达的地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用我工作,的特点之一是,当他们到达,他们已经事先设置位置通知用户。

In the application I'm working on, one of the features is to notify the user when they reach a location that they have set prior.

下code为addProximityAlert的活动:

The code below is in addProximityAlert in Activity:

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");
    }
}
}

它工作正常,但我需要调用 unregisterReceiver 每一次我破坏活动 - 这意味着我的应用程序不再通知用户。但我想,直到他取消,或者已经收到通知,即使他们关闭应用程序通知用户的时候,他是附近的位置。

It works fine, but I need to call unregisterReceiver every single time I destroy the Activity - that means my application no longer notifies the user. But I want to notify the user when he is near the location until he cancels, or is already notified even if they close the app.

我是什么失踪?

推荐答案

所有我需要做的就是在清单中定义接收器

all i need to do is define receiver in Manifest

然后我不需要注册/注销活动中再

then i don't need to register/unregister in Activity anymore

  <receiver android:name="th.clbs.android.broadcastreceiver.LocationReminderReceiver" >
        <intent-filter>
            <action android:name="th.co.clbs.action.LOCATION_REMINDER" />
        </intent-filter>
    </receiver>

这篇关于通知用户,当他们到达的地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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