在某个位置附近生成通知

时间:2012-04-29 19:01:52

标签: android location

我正在尝试构建一个代码,当用户靠近某个位置时会启动通知。我搜索了关于该主题,但无法找到解决方案。我使用以下代码,但它无法正常工作。

下面的代码是我的Location类,其内部类是MyLocationListener,它实现了LocationListener

`@Override       public void onLocationChanged(Location loc)
      {
          CharSequence from =“AlarmManager - Time up 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();
  }

对此主题的任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:4)

  

我正在尝试构建一个代码,当用户靠近某个位置时会启动通知。

使用LocationListeneraddProximityAlert()