2014-04-19 3 views
0

Android 가상 장치에서 WiFi를 통해 현재 위치를 가져 오려고하는데이 프로그램을 디버깅 할 때 수동 네트워크 공급자의 위치가 Null 인 경우 getLocation()에 표시됩니다. 위치 관리자에게 값어치가 있습니다. 이 내 내 gpsProviderNetworkProvider 모두 거짓 값을 반환하고 공급자는 수동입니다.안드로이드의 현재 위치의 위도와 경도?

import java.util.List; 

import android.content.Context; 
    import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 

public class ShowLocation extends FragmentActivity 
{ 
Context context; 
private LocationManager locationManager=null; 
private LocationListener locationListener=null; 
private Location location=null; 
double latitude,longitude; 
boolean gpsStatus=false,NetworkProvider=false,Status=false,Passiveprovider=false; 
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

// The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 
private Criteria criteria=null; 
    String Provider=null; 
public void onCreate(Bundle state) 
{ 
    super.onCreate(state); 
    setContentView(R.layout.fragment_main); 
    context=getApplicationContext(); 
    if(Status=checkStatus()) 
    { 

     getLocation(); 
    } 
} 

public boolean checkStatus() 
{ 
    boolean status1=false; 
    locationManager=(LocationManager)getSystemService(LOCATION_SERVICE); 
    List<String> provider =locationManager.getAllProviders(); 
    System.out.println("Location Manager ="+provider); 
    for(String providername : provider) 
     { 
      if(providername.equals(LocationManager.GPS_PROVIDER)) 
       { 
        gpsStatus=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
        status1=true; 
       } 
      if(providername.equals(LocationManager.NETWORK_PROVIDER)) 
       { 
        NetworkProvider=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
        status1=true; 
       } 
      if(providername.equals(LocationManager.PASSIVE_PROVIDER)) 
       { 
        Passiveprovider=locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER); 
        status1=true; 
       } 
     } 


System.out.println("Network provider ="+NetworkProvider + "Gps Provider ="+gpsStatus + "PassiveProvider ="+Passiveprovider); 

    return status1; 


} 
public void getLocation() 
{ 
    locationListener=new LocationLis(); 
    if(gpsStatus) 
    { 
     System.out.println("Gps Provider = "+gpsStatus); 
     Log.d("Gps is on","=" +gpsStatus); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 
     criteria=new Criteria(); 
     Provider=locationManager.getBestProvider(criteria, true); 
     if(locationManager !=null) 
      { 
       location=locationManager.getLastKnownLocation(Provider); 
       if(location != null) 
        { 
         latitude=location.getLatitude(); 
         longitude=location.getLongitude(); 
         System.out.println("Gps Provider"); 
         System.out.println(latitude +" "+longitude); 
        } 

      } 
    } 
    else if(NetworkProvider) 
     { 
     System.out.println("NetworkProvider"+NetworkProvider); 
      Log.d("Network is on", "="+NetworkProvider); 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener); 
      criteria=new Criteria(); 
      Provider=locationManager.getBestProvider(criteria, true); 
      if(locationManager !=null) 
      { 
       location=locationManager.getLastKnownLocation(Provider); 
       if(location !=null) 
       { 
        latitude=location.getLatitude(); 
        longitude=location.getLongitude(); 
        System.out.println("Network Provider"); 
        System.out.println(latitude +" "+longitude); 

       } 
      } 
     } 
    else if(Passiveprovider) 
     { 

      System.out.println("PassiveProvider"+Passiveprovider); 
       Log.d("Network is on", "="+Passiveprovider); 
       locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,0,0,locationListener); 
       criteria=new Criteria(); 
       Provider=locationManager.getBestProvider(criteria, true); 
       // Here Provider is null 

       if(locationManager !=null) 
       { 

        location=locationManager.getLastKnownLocation(Provider); 
        if(location !=null) 
        { 
         latitude=location.getLatitude(); 
         longitude=location.getLongitude(); 
         System.out.println("Network Provider"); 
         System.out.println(latitude +" "+longitude); 

        } 
       } 
     } 
    else 
     { 
      System.out.println("Provider r not available"); 
     } 

} 

개인 클래스 LocationLis는

android:glEsVersion="0x00020000" 
android:required="true" /> 

<uses-feature 
    android:name="android.hardware.wifi" 
    android:required="true" /> 

<permission 
    android:name="com.Priyank.priyankcurrentlocation.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.Priyank.priyankcurrentlocation.MAPS_RECEIVE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> 
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER" /> 
<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" /> 

<meta-data 
    android:name="com.google.android.maps.v2.API_KEY" 
    android:value="I have key with me." /> 

답변

0

getLastKnownLocation이 null을 반환하도록 허용 LocationListener {

@Override 
    public void onLocationChanged(Location location) 
    { 

    } 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 


} 

    } 





} 

Manifest.xml 파일을 구현합니다. 최근에 위치를 파악하지 못했거나 위치가 너무 오래되었으므로 반환하지 않으려한다는 의미입니다. 이 가능성을 고려해야합니다. 그것은 getLastKnownLocation에주의해야하는 두 가지 이유 중 하나입니다 (다른 하나는 값을 반환해도 실제로는 시간이 오래 걸리므로 신선도가 보장되지 않습니다).

+0

이제는 어떤 것들이 함께 사용되었는지 알 수 있습니다. – user3509581

+0

어떻게 내가 천천히 그것을 제안 해주세요. – user3509581