通知附近的位置

时间:2012-04-29 05:11:22

标签: android gps

我想知道当他在特定位置附近时是否有任何方式通知用户 当用户靠近该位置时,通知会发送到他的电话,提醒他他的位置。

我正在尝试使用以下代码,但它无效。

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

}

3 个答案:

答案 0 :(得分:1)

答案是肯定的。

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

GPS消耗大量电池电量,因此您最好首先使用网络位置提供商,然后当它离该位置不远时,启动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输出。

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

答案 1 :(得分:0)

检查这些链接以获取更多详细信息:

LocationListener

Obtaining User Location

答案 2 :(得分:0)

此处位置更改代码..尝试此。

@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; 
    } 

根据您的代码,您需要根据需要进行更改....