2012-04-29 202 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