2015-02-06 3 views
0

나는 안드로이드에서 새로운 편이다. 나는 그의 사무실이 자신의 사무실이고 그의 사무실도 사무실이라면 "사무실에 오신 것을 환영합니다"라는 알림을받는 작은 앱을 개발할 것입니다. 그의 집이 그의 집이라면 집. 그 일을하기 위해 내가 무엇을해야하는지 말해주세요.사용자가 집이나 사무실에있을 때 알림을 얻는 방법

+0

Google에서 "Geofencing android"검색 –

+0

@FlamePrincess 이것은 내가 찾고있는 것입니다. 몇 가지 예가 나와 있으므로 참조하고 배울 수 있습니다. –

+0

감사합니다 @ FlamePrincessϡ 귀하의 의견에서 좋은 힌트를 얻을. 나는 또한 존경받는 프로그램 코드를 얻는다. "지오 펜싱"에 대해서는 잘 모르지만 매우 유용하고 중요해 보입니다. 조금만 말해 주시겠습니까? 내게 도움이되는 링크는 다음과 같습니다. http://www.javacodegeeks.com/2011/01/android-proximity-alerts-tutorial.html –

답변

1

사용 당신이 호출 아래 코드

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.provider.Settings; 

import com.gs.mobile.Constants; 
import com.gs.mobile.R; 

public class MyLocation { 
    private Timer timer1; 
    private LocationManager lm; 
    private LocationResult locationResult; 
    private boolean gps_enabled=false; 
    private boolean network_enabled=false; 
    private AlertDialog.Builder dialog; 

    public boolean getLocation(final Context context, LocationResult result){ 
     //I use LocationResult callback class to pass location value from MyLocation to user code. 
     locationResult=result; 
     if(lm==null) 
      lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

     //exceptions will be thrown if provider is not permitted. 
     try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} 
     try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} 

     //don't start listeners if no provider is enabled 
     if(!gps_enabled && !network_enabled){ 
      dialog = new AlertDialog.Builder(context); 
      dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled)); 

      dialog.show(); 

     } 
     if(gps_enabled) 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); 
     if(network_enabled) 
      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); 
     timer1=new Timer(); 
     timer1.schedule(new GetLastLocation(), 20000); 
     return true; 
    } 

    LocationListener locationListenerGps = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerNetwork); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    LocationListener locationListenerNetwork = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerGps); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    class GetLastLocation extends TimerTask { 
     @Override 
     public void run() { 
      lm.removeUpdates(locationListenerGps); 
      lm.removeUpdates(locationListenerNetwork); 

      Location net_loc=null, gps_loc=null; 
      if(gps_enabled) 
       gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if(network_enabled) 
       net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

      //if there are both values use the latest one 
      if(gps_loc!=null && net_loc!=null){ 
       if(gps_loc.getTime()>net_loc.getTime()) 
        locationResult.gotLocation(gps_loc); 
       else 
        locationResult.gotLocation(net_loc); 
       return; 
      } 

      if(gps_loc!=null){ 
       locationResult.gotLocation(gps_loc); 
       return; 
      } 
      if(net_loc!=null){ 
       locationResult.gotLocation(net_loc); 
       return; 
      } 
      locationResult.gotLocation(null); 
     } 
    } 

    public static abstract class LocationResult{ 
     public abstract void gotLocation(Location location); 
    } 
}//MyLocation-class 

도움이 될 것입니다 희망 저장할 다른 용도로이 위치를 저장하는 사용자에게 옵션을 제공 sholud 확인 위도 및 LANG 당신의 공공 무효 gotLocation (위치 위치) 메소드가 호출 될 것이다 변경 때 활동으로

/* Used to Get Device GPS Location 
    * <uses-permission android:name=�android.permission.ACCESS_FINE_LOCATION�> 
    */ 
    public static LocationResult locationResult = new LocationResult(){ 

     @Override 
     public void gotLocation(Location location) { 
      // TODO Auto-generated method stub 

     } 
    }; 

    public static void getGPS(Activity _activity){ 
     MyLocation myLocation = new MyLocation(); 
     myLocation.getLocation(_activity, locationResult); 

    }//getGPS() 

    public static void getGPS(Activity _activity,LocationResult locationResult){ 
     MyLocation myLocation = new MyLocation(); 
     myLocation.getLocation(_activity, locationResult); 

    }//getGPS() 

아래마다 표시됩니다 어디에서 수행 할 수 있습니다 위치가 예상보다 아래에 있는지 확인하십시오.

당신이 원하는 경우

, 심지어 당신이 구글에서 서비스에 전송하여 해당 위도, 랭의 이름을 얻을 수있는 URL http://maps.googleapis.com/maps/api/geocode/xml?address=37.4188514,-122.0874526&sensor=true

를 사용할 수있다.

+0

현재 로직과 위도가 항상 매번 바뀌기 때문에이 로직은 더 많은 배터리를 소모합니다. – Gautam

+0

@ user3585230 맞습니다. 그렇지만 외부 센서를 사용하지 않고이 작업을 수행하는 방법입니다. 그렇지 않으면 라우터의 MAC 주소를 사용하는 wifi 연결을 사용하거나 iBeacons를 사용하는 다른 더 좋은 방법이 있습니다. – Salmaan

0

GPS을 사용하려면 위치와 관련하여 알림을 설정해야합니다. 하지만 ofice 가정 또는

그것이

+0

예 @Sukhwant 나는 그것을 얻었지만 그런 프로그램에서 Service를 사용해야한다는 것을 알고 싶습니까? 전체 프로그램이 백그라운드에서 자신의 작업을하고 있기 때문입니다. –

+1

예 백그라운드 작업을 위해 @ULHASPATIL 서비스가 필요합니다. –

2

사용자가이 영역과 종료 영역을 입력 할 때 지오 펜스지리 정보를 완벽하게 작성하십시오. 보류중인 인 텐트의 고유 ID를 제공합니다.

관련 문제