我想知道是否有任何方法可以在用户靠近特定位置时通知他。
当用户靠近该位置时,他的手机会收到一条通知,提醒他自己的位置。
我正在尝试以下代码,但它不起作用。
public class NotifyuserActivity extends Activity {
LocationManager locMan;
double lat;
double lon;
NotificationManager notMan;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locMan=(LocationManager) getSystemService(LOCATION_SERVICE);
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10, mylistener);
}
LocationListener mylistener=new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
lat = location.getLatitude();
lon = location.getLongitude();
CharSequence from = "AlarmManager - Time's up!";
CharSequence message = "This is your alert";
Location tasklocation=new Location(LocationManager.GPS_PROVIDER);
double dis1 = location.distanceTo(tasklocation);
tasklocation.setLatitude(22.727733);
tasklocation.setLongitude(75.886079);
if(dis1 < 200.00)
{
notMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.ic_launcher, "Click here for details", System.currentTimeMillis());
Intent notificationIntent = new Intent(NotifyuserActivity.this,NotifyuserActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(NotifyuserActivity.this, 0, notificationIntent, 0);
n.setLatestEventInfo(NotifyuserActivity.this, from,message, pendingIntent);
notMan.notify(10001, n);
Toast.makeText(NotifyuserActivity.this,"Distance to location is: " + dis1, Toast.LENGTH_LONG).show();
}
}
};
}