2012-11-13 5 views
0

저는 안드로이드 프로그램에서 새로운데, 내 응용 프로그램에 문제가 있습니다. 내 Gps가 위치 또는 다른 것을 검색하지 않습니다. 네, 내 GPS가 켜져 있습니다. 매니페스트는 퍼밋을 사용합니다 : ACCESS_COARSE_LOCATION 및 ACCESS_FINE_LOCATION.GPS가 내 안드로이드에서 검색하지 않습니다.

누군가 나를 도울 수 있습니까?

public class LocationTest extends Activity implements 
    LocationListener { 
private static final String[] A = { "invalid", "n/a", "fine", "coarse" }; 
private static final String[] P = { "invalid", "n/a", "low", "medium", 
    "high" }; 
private static final String[] S = { "out of service", 
    "temporarily unavailable", "available" }; 

private LocationManager mgr; 
private TextView output; 
private String best; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
    output = (TextView) findViewById(R.id.output); 

    log("Location providers:"); 
    dumpProviders(); 

    Criteria criteria = new Criteria(); 
    best = mgr.getBestProvider(criteria, true); 
    log("\nBest provider is: " + best); 

    log("\nLocations (starting with last known):"); 
    if (best != null) { 
    Location location = mgr.getLastKnownLocation(best); 
    dumpLocation(location); 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    // Start updates (doc recommends delay >= 60000 ms) 
    if (best != null) { 
    mgr.requestLocationUpdates(best, 15000, 1, this); 
    } 
    } 

@Override 
protected void onPause() { 
    super.onPause(); 
    // Stop updates to save power while app paused 
    mgr.removeUpdates(this); 
} 

public void onLocationChanged(Location location) { 
    dumpLocation(location); 
} 

public void onProviderDisabled(String provider) { 
    log("\nProvider disabled: " + provider); 
} 

public void onProviderEnabled(String provider) { 
    log("\nProvider enabled: " + provider); 
} 

public void onStatusChanged(String provider, int status, 
    Bundle extras) { 
    log("\nProvider status changed: " + provider + ", status=" 
     + S[status] + ", extras=" + extras); 
} 

/** Write a string to the output window */ 
private void log(String string) { 
    output.append(string + "\n"); 
} 

/** Write information from all location providers */ 
private void dumpProviders() { 
    List<String> providers = mgr.getAllProviders(); 
    for (String provider : providers) { 
    dumpProvider(provider); 
    } 
} 

/** Write information from a single location provider */ 
private void dumpProvider(String provider) { 
    LocationProvider info = mgr.getProvider(provider); 
    StringBuilder builder = new StringBuilder(); 
    builder.append("LocationProvider[") 
     .append("name=") 
     .append(info.getName()) 
     .append(",enabled=") 
     .append(mgr.isProviderEnabled(provider)) 
     .append(",getAccuracy=") 
     .append(A[info.getAccuracy() + 1]) 
     .append(",getPowerRequirement=") 
     .append(P[info.getPowerRequirement() + 1]) 
     .append(",hasMonetaryCost=") 
     .append(info.hasMonetaryCost()) 
     .append(",requiresCell=") 
     .append(info.requiresCell()) 
     .append(",requiresNetwork=") 
     .append(info.requiresNetwork()) 
     .append(",requiresSatellite=") 
     .append(info.requiresSatellite()) 
     .append(",supportsAltitude=") 
     .append(info.supportsAltitude()) 
     .append(",supportsBearing=") 
     .append(info.supportsBearing()) 
     .append(",supportsSpeed=") 
     .append(info.supportsSpeed()) 
     .append("]"); 
    log(builder.toString()); 
} 

/** Describe the given location, which might be null */ 
private void dumpLocation(Location location) { 
    if (location == null) 
    log("\nLocation[unknown]"); 
    else 
    log("\n" + location.toString()); 
} 

} 
+1

네트워크는 어떻게됩니까? 그들은 당신의 위치를보고하고 있습니까? GPS 센서는 건물 안에있는 위치를보고하지 않는다는 것을 알아야합니다. 야외에서만 작동합니다. –

+1

.. 처음 실행하면 시간이 많이 걸릴 것입니다. 또한 Logcat 출력을 게시하십시오. – varevarao

+0

답변을 작성 했으므로 지금 집으로 돌아가서 오늘까지 충분히 했으므로 여전히 필요하다면 내일 도울 것입니다. (원한다면이 프로젝트를 내일 다운로드 할 수 있습니다). – Bigflow

답변

0

저는 일반적으로 이렇게하지는 않지만 거의해야합니다. 이것은 내가 사용하는 코드입니다. (단지 이것을 새로운 프로젝트에 넣으십시오). 나는 내 다른 프로젝트에서 그것을 찢어 때문에 나는 그것을 청소하지 못했지만, 새 프로젝트를 만들고 단지/복사 할 때이 작품은, 붙여 넣기이

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


import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.widget.Toast; 


public class MainActivity extends Activity { 
    Timer timer1; 
    LocationManager lm; 
    boolean gps_loc = false; 
    boolean gps_enabled=false; 
    boolean network_enabled=false; 
    double lat; 
    double lng; 
    String gps_location; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     getLocation(this, locationResult); 
    } 

    public LocationResult locationResult = new LocationResult() { 
     public void gotLocation(final Location location) { 
      try { 
       lat = location.getLatitude(); 
       lng = location.getLongitude(); 
       if (lat != 0.0 && lng != 0.0) { 
        String sLat; 
        String sLng; 
        sLat = Double.toString(lat); 
        sLng = Double.toString(lng); 
        gps_location = sLat + " " + sLng; 
        Toast.makeText(getBaseContext(), "We got gps location!", 
          Toast.LENGTH_LONG).show(); 
        System.out.println("We got gps"); 
        System.out.println("lat = "+lat); 
        System.out.println("lng = "+lng); 
       } 
      } catch (Exception e) { 

      } 
     } 
    }; 

    public boolean getLocation(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){ 

      return false; 
     } 
     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(), 35000); 

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

} 

이 또한 매니페스트이 추가 .: 않습니다

<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" /> 

아직 설명 할 시간이 없으며, 여전히 필요할 경우 내일입니다.

logcat에 위도와 경도를 인쇄합니다.

+0

사실,이 응용 프로그램은 버전 2.3 (Froyo)에서 작동하지만 내 Jelly bean 또는 Froyo보다 높은 버전에서는 작동하지 않습니다. =/ 누군가 이미 이와 같은 문제가 있었습니까? –

+0

@KleberRibeiro 그것은 내 작품 (4.0.4), 어떤 특정 오류를주지 않는가요? 곧 전화로 다시 테스트 해 보겠습니다. 내가 잠 들어있을 때 목욕탕에 내 전화기를 떨어 뜨렸다. 먼저 수정해야합니다. – Bigflow

+0

오류가 없습니다. GPS가 검색하지 않습니다. 화면 상단에 나타나는 아이콘을 알고 있습니까? 그래서, 그것은 나타나지 않습니다 –

관련 문제