这是我在这里的第一个问题,我搜索答案但没有找到。我构建了一个简单的应用程序,它可以获取当前位置,如果我站在特定位置会发出警报,但警报没有触发,请帮忙。这是我的代码:
主要活动:
package com.lda.notyalert;
public class MainActivity extends Activity {
ProximityIntentReceiver proximityIntentReceiver;
private static final String PROXIMITY_ALERT = "com.lda.notyalert.ProximityIntentReceiver";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager;
String svcName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(svcName);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);
Location l = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(l);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
setProximityAlert();
IntentFilter filter = new IntentFilter(PROXIMITY_ALERT);
proximityIntentReceiver = new ProximityIntentReceiver();
registerReceiver(proximityIntentReceiver, filter);
}
private void setProximityAlert() {
String locService = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager = (LocationManager) getSystemService(locService);
double lat = 37.422006;
double lng = -122.084095;
float radius = 100f; // meters
long expiration = -1; // do not expire
Intent intent = new Intent(PROXIMITY_ALERT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1,
intent, 0);
locationManager.addProximityAlert(lat, lng, radius, expiration,
proximityIntent);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText;
myLocationText = (TextView) findViewById(R.id.textView1);
String latLongString = "No location found";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}
myLocationText.setText("Your Current Position is:\n" + latLongString);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
}
这是扩展 BroadcastReceiver 的 ProximityIntentReceiver;
package com.lda.notyalert;
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
} else {
Log.d(getClass().getSimpleName(), "exiting");
}
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
null, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(context, "Proximity Alert!",
"You are near your point of interest.", pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
private Notification createNotification() {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
return notification;
}
}