2012-04-29 120 views
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的=新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(); 
     } 
    } 
}; 

}

+0

我对此并不熟悉,但在测量距离后,您设置了请求的位置(即taskLocation.setLat \ setLong ...)。 另外,你只说,它不工作。它有什么作用?什么工作? – 2012-04-29 07:20:28

+0

实际上我想在我的项目中使用此模块。亚我已经尝试编写tasklocation以上DIS,但它也不工作 – shubh 2012-04-29 08:36:35

回答

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

你需要改变其中需要按您的代码....

+0

我无法理解你的代码,这将有助于如果你找出我的代码中的错误... – shubh 2012-04-29 08:48:22

1

答案是肯定的。

您可以使用一些时间或距离间隔设置注册LocationListenerLocationManager,然后当位置发生变化时,可以将其与您设置的决定是否通知用户进行比较。

GPS会消耗大量电池电量,所以您应该首先使用网络位置提供程序,然后在距离该位置不太远时启动GPS。


正如评论说,你应该首先移动

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

上述

double dis1 = location.distanceTo(tasklocation);

(更好的,你可以将这些位置列明的onLocationChanged(),而不是硬编码纬度/经度)。

Th可能无法解决你的问题,但不这样做将永远不会使其工作。所以不要留下那样的错误。


然后,我应该说此代码的工作,我(在我的模拟器),我不能看到其他错误,你所付出的,可能会给你“不工作”。

您可核对权限

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

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

毕竟,关于你的“不工作”的更多细节。

+0

是的,我已经尝试过,但代码无法正常工作。 – shubh 2012-04-29 08:49:03

+0

我已经添加了权限并进行了更改,但代码仍然无法正常工作。当我到达位置时在手机上测试代码时,没有通知。此代码在模拟器上正常工作。 – shubh 2012-04-29 15:44:10

+0

向'onLocationChanged()'添加一些代码,以显示每个位置变化的“dis1”(例如,输出到“TextView”)。显示结果。 – 2012-04-29 16:30:06