0

我想知道是否有任何方法可以在用户靠近特定位置时通知他。
当用户靠近该位置时,他的手机会收到一条通知,提醒他自己的位置。

我正在尝试以下代码,但它不起作用。

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

}

4

2 回答 2

1

这里位置更改代码..试试这个。

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    if (location != null) {
        System.out.println("in onlocationchanged");
         String locationString=location.convert(location.getLatitude(),1);
         Toast.makeText(this,"locationString=="+locationString, Toast.LENGTH_LONG).show();
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        String currentLocation = "The location is changed to Lat: " + lat + " Lng: " + lng;
        Toast.makeText(this,currentLocation, Toast.LENGTH_LONG).show();
        p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);


        // check in database if we have same lattitude & longitude

        MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
        Log.d("Reading: ", "Reading all contacts..");
        List<LocWiseProfileBeans> LocWiseProfile= m.getAllLocWiseProfile();       


        for (LocWiseProfileBeans cn : LocWiseProfile) {
            String log = "Loc Name: "+cn.getLocname()+" ,Lattitude: " + cn.getLattitude()+ " ,Longitude: " + cn.getLongitude()+ " , Selected Profile :"+cn.getSelectedprofile();
                // Writing Contacts to log
            double distance2=00.00;

      GeoPoint storedLocation=new GeoPoint(
      (int) cn.getLattitude() * 1000000,(int) cn.getLongitude() * 1000000);    

        Location locationB = new Location("point B");  

        locationB.setLatitude((int) cn.getLattitude() * 1000000);  
        locationB.setLongitude((int) cn.getLongitude() * 1000000);  


        distance2=distFrom(lat,lng,cn.getLattitude(),cn.getLongitude());


        if(distance2>1 )
        {
            Log.d("Name: ", log);
            Toast.makeText(this, "Loc Name:"+log, Toast.LENGTH_SHORT).show();
            Toast.makeText(this,"identical", Toast.LENGTH_LONG).show();
            Toast.makeText(this,"distance2======"+distance2, Toast.LENGTH_SHORT).show();
sendnotification("Locale Notifications","You visited location " + cn.getLocname());


        }

此代码用于通知用户。

protected void sendnotification (String title, String message) {
       String ns = Context.NOTIFICATION_SERVICE;
       NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

       int icon = R.drawable.icon;
       CharSequence tickerText = message;
       long when = System.currentTimeMillis();

       Notification notification = new Notification(icon, tickerText, when);

       Context context = getApplicationContext();
       CharSequence contentTitle = title;
       CharSequence contentText = message;
       Intent notificationIntent = new Intent(this, GoogleMapsActivity.class);
       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

       notification.flags = Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
       mNotificationManager.notify(1, notification);
    }

这是将经度和纬度转换为英里的功能。

public static double distFrom(double lat1, double lng1, double lat2, double lng2) { 
      double earthRadius = 3958.75; 
      double dLat = Math.toRadians(lat2-lat1); 
      double dLng = Math.toRadians(lng2-lng1); 
      double a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
               Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
               Math.sin(dLng/2) * Math.sin(dLng/2); 
      double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      double dist = earthRadius * c; 

      return dist; 
    } 

您需要根据您的代码在需要的地方进行更改....

于 2012-04-29T05:41:32.597 回答
1

答案是肯定的。

您可以注册一个带有一些时间或距离间隔设置的LocationListenerto LocationManager,然后当位置更改时,可以将其与您设置的内容进行比较,以决定是否通知用户。

GPS消耗大量电池电量,因此您最好先使用Network Location Provider,然后在距离该位置不远时启动GPS。


正如评论所说,你应该先行动

tasklocation.setLatitude(22.727733);
tasklocation.setLongitude(75.886079);

以上

double dis1 = location.distanceTo(tasklocation);

(更好的是,您可以将这些位置设置移出onLocationChanged()而不是硬编码纬度/经度。)

这可能无法解决您的问题,但不这样做将永远无法解决问题。所以不要留下这样的错误。


然后,我应该说这段代码对我有用(在我的模拟器中),我看不出你给出的其他错误可能会让你“不工作”。

您可以检查您的权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

或者可以显示Toast结果或Logcat输出。

毕竟,有关您的“不工作”的更多详细信息。

于 2012-04-29T05:45:31.980 回答