我正在尝试构建一个代码,当用户靠近某个位置时启动通知。我用谷歌搜索了这个主题,但找不到解决方案。我正在使用以下代码,但它不起作用。
下面的代码是我的 Location 类,它的内部类是 MyLocationListener,它实现了 LocationListener
` @Override public void onLocationChanged(Location loc)
{
CharSequence from = "AlarmManager - Time's up!";
CharSequence message = "这是你的警报";
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
float radius = 500; //Distance in meters
Location location1 = new Location("GPS");
location1.setLatitude(loc.getLatitude());
location1.setLongitude(location1.getLongitude());
Location location2 = new Location("GPS");
location2.setLatitude(22.727739);
location2.setLongitude(75.886074);
Float distance = location1.distanceTo(location2);
distance = Math.abs(distance);
if(distance < radius)
{
Log.d("sdsd", "task location");
notMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.ic_launcher, "Click here for details", System.currentTimeMillis());
Intent notificationIntent = new Intent(Location_Gps.this,Location_Gps.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Location_Gps.this, 0, notificationIntent, 0);
n.setLatestEventInfo(Location_Gps.this, from,message, pendingIntent);
notMan.notify(10001, n);
}
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
任何有关该主题的帮助表示赞赏。