2013-11-20 1 views
0

У меня есть приложение, которое используется для обнаружения местоположения пользователя с этим кодомAndroid GoogleMaps v1 LocationManager работает нечетным на 4.4 KitKat

public static Location getCurrentLocation(Context context) { 
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    Criteria c = new Criteria(); 
    String provider = lm.getBestProvider(c, true); 
    Location location; 
    if (provider == null) { 
     location = new Location("gps"); 
     location.setLatitude(U.LATITUDE); 
     location.setLongitude(U.LONGITUDE); 
    } else { 
     location = lm.getLastKnownLocation(provider); 
     if (location == null) { 
      location = new Location("gps"); 
      location.setLatitude(U.LATITUDE); 
      location.setLongitude(U.LONGITUDE); 
     } 
    } 
    return location; 
} 

Этот код появляется, возвращается только поведение, когда поставщик является нулевым или расположение нуля , Я предполагаю, что в этом коде что-то изменилось.

String provider = lm.getBestProvider(c, true); 
+0

Где ошибка? –

+0

Я попытаюсь найти ошибку, но до сих пор я ее не нашел. Тем не менее, я заметил, что когда я отключу GPS-услуги, он подходит к месту, близкому моему местоположению. Когда я поворачиваю GPS обратно, он переходит к моему местоположению GPS по умолчанию в коде. – wapples

ответ

0

Здесь возьмите мой код, чтобы получить долготу и широту. Это весь проект. Взгляни. Он отлично работает. Однако вы должны включить GPS самостоятельно, потому что выше версии 4.0 вы не можете включить gps программно

Используйте мой код, чтобы получить его. Включите GPS и Wi-Fi раньше: -

Используйте мой код. Я успешно нашел свой текущий адрес, используя координаты, но для этого вам нужно включить Wi-Fi и gps. сначала добавить все эти разрешения на андроид файл манифеста

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

AndroidGPSTrackingActivity.java *

package com.example.gpstracking; 

import java.io.IOException; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.util.List; 
import java.util.Locale; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.LocationManager; 
import android.net.ConnectivityManager; 
import android.net.Uri; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.view.View; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.Toast; 
import android.widget.ToggleButton; 

public class AndroidGPSTrackingActivity extends Activity { 

    Button btnShowLocation; 

    // GPSTracker class 
    GPSTracker gps; 
    ToggleButton gpstoggle,wifitoggle,datatoggle; 
    static WifiManager wifiManager; 
    LocationManager lm; 
    WebView web; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnShowLocation = (Button) findViewById(R.id.btnShowLocation); 

     gpstoggle = (ToggleButton) findViewById(R.id.gps); 
     wifitoggle = (ToggleButton) findViewById(R.id.wifi); 

     wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 

     lm = (LocationManager) getSystemService(LOCATION_SERVICE); 

     //gps toggle 
     gpstoggle.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked){ 
        final Intent poke = new Intent(); 
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
        poke.setData(Uri.parse("3")); 
        getApplicationContext().sendBroadcast(poke); 
       } 
       else{ 
        final Intent poke = new Intent(); 
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
        poke.setData(Uri.parse("3")); 
        getApplicationContext().sendBroadcast(poke); 
       } 
      } 
     }); 
     //wifi toggle 
     wifitoggle.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked){ 
        wifiManager.setWifiEnabled(true); 
       } 
       else{ 
        wifiManager.setWifiEnabled(false); 
       } 
      } 
     }); 

     //data toggle 
     datatoggle.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked){ 
        ConnectivityManager dataManager; 
        dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
        Method dataMtd = null; 

        try { 
         dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); 
         dataMtd.setAccessible(true); 
         dataMtd.invoke(dataManager, true); 

        } catch (NoSuchMethodException e1) { 
         e1.printStackTrace(); 
        } 
        catch (IllegalArgumentException e) { 
         e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
         e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
         e.printStackTrace(); 
        } 
        catch (SecurityException e) { 
         e.printStackTrace(); 
        } 

       } 
       else{ 
        ConnectivityManager dataManager; 
        dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
        Method dataMtd = null; 

        try { 
         dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); 
         dataMtd.setAccessible(false); 
         dataMtd.invoke(dataManager, false); 


        } catch (NoSuchMethodException e1) { 
         e1.printStackTrace(); 
        } 
        catch (IllegalArgumentException e) { 
         e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
         e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
         e.printStackTrace(); 
        } 
        catch (SecurityException e) { 
         e.printStackTrace(); 
        } 

       } 
      } 
     }); 

     // show location button click event 
     btnShowLocation.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) {   
       // create class object 
       gps = new GPSTracker(AndroidGPSTrackingActivity.this); 

       // check if GPS enabled  
       if(gps.canGetLocation()){ 

        double latitude = gps.getLatitude(); 
        double longitude = gps.getLongitude(); 

        Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault()); 
        List<Address> addresses = null; 
        try { 
         addresses = gcd.getFromLocation(latitude, longitude, 1); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        if(!(addresses == null)){ 
         if (!addresses.isEmpty()){ 
          System.out.println(addresses.get(0).getLocality()); 

          Address address = addresses.get(0); 

          String addressText = String.format("%s, %s, %s, %s, %s, %s", 
            address.getAddressLine(0), 
            address.getAddressLine(1), 
            address.getAddressLine(2), 
            address.getAddressLine(3), 
            address.getPhone(), 
            address.getPremises()); 

          System.out.println(addressText); 

          // \n is for new line 
          Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
         }else{ 
          // can't get location 
          // GPS or Network is not enabled 
          // Ask user to enable GPS/network in settings 
          gps.showSettingsAlert(); 
         } 
        }else{ 
         // can't get location 
         // GPS or Network is not enabled 
         // Ask user to enable GPS/network in settings 
         gps.showSettingsAlert(); 
        } 

       } 

      } 
     }); 
    } 

} 

GPSTracker.java

package com.example.gpstracking; 

import android.app.AlertDialog; 
import android.app.Service; 
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.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 
import android.util.Log; 
import android.widget.Toast; 

public class GPSTracker extends Service implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    Location location; // location 
    double latitude; // latitude 
    double longitude; // longitude 

    // The minimum distance to change Updates in meters 
    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 

    // Declaring a Location Manager 
    protected LocationManager locationManager; 

    public GPSTracker(Context context) { 
     this.mContext = context; 
     getLocation(); 
    } 

    public Location getLocation() { 
     try { 
      locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      boolean wifiEnabled = AndroidGPSTrackingActivity.wifiManager.isWifiEnabled(); 

      ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 

      this.canGetLocation = true; 

      // if DATA Enabled get lat/long using WIFI Services 
      if (activeNetwork.isConnected()) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

       } 
      } 
      // if WIFI Enabled get lat/long using WIFI Services 
      if (wifiEnabled) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS Enabled", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 

        } 
       } 
      } 
      if (location != null) { 
       latitude = location.getLatitude(); 
       longitude = location.getLongitude(); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return location; 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    * */ 
    public void stopUsingGPS(){ 
     if(locationManager != null){ 
      locationManager.removeUpdates(GPSTracker.this); 
     }  
    } 

    /** 
    * Function to get latitude 
    * */ 
    public double getLatitude(){ 
     if(location != null){ 
      latitude = location.getLatitude(); 
     } 

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude(){ 
     if(location != null){ 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog 
    * On pressing Settings button will lauch Settings Options 
    * */ 
    public void showSettingsAlert(){ 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

     // Setting Dialog Title 
     alertDialog.setTitle("GPS is settings"); 

     // Setting Dialog Message 
     alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

     // On pressing Settings button 
     alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
      } 
     }); 

     // on pressing cancel button 
     alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

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

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button android:id="@+id/btnShowLocation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Show Location" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true"/> 

    <ToggleButton 
     android:id="@+id/gps" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btnShowLocation" 
     android:layout_alignRight="@+id/btnShowLocation" 
     android:layout_marginBottom="49dp" 
     android:layout_marginRight="72dp" 
     android:text="GPS" 
     /> 

    <ToggleButton 
     android:id="@+id/wifi" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/gps" 
     android:layout_alignBottom="@+id/gps" 
     android:layout_alignLeft="@+id/btnShowLocation" 
     android:layout_marginLeft="79dp" 
     android:text="WIFI" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/gps" 
     android:layout_alignLeft="@+id/btnShowLocation" 
     android:text="GPS" 
     android:textSize="15sp" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_marginLeft="61dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:text="WIFI" 
     android:textSize="15sp" /> 

</RelativeLayout> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<uses-sdk android:minSdkVersion="8" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".AndroidGPSTrackingActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

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

</manifest> 
+0

Спасибо, я собираюсь проверить часть этого кода и посмотреть, исправляет ли он мою проблему. Я думаю, что уже видел этот код в Интернете, но я мог ошибаться. – wapples

+0

Я реализовал getLocation() в своем приложении и, похоже, работал над 4.4. – wapples

+0

Что происходит, когда у вас нет Wi-Fi или GPS и только на данных? Я видел некоторые проблемы с этим сценарием. – wapples

3

У меня есть некоторый код, очень похожий на ваш, и он перестал работать на устройстве после обновления до Android 4.4 (т.е. getBestProvider возвращает null, который он никогда не делал раньше).

Я обнаружил причину, когда я увидел, что другое приложение показало, что NetworkProvider недоступен (хотя у меня было идеальное соединение с Wi-Fi и 3G), и он не мог получить сигнал GPS (очевидно, потому что я был в помещении) , Затем я переключил настройку местоположения Android на Android, и появилось диалоговое окно с просьбой согласиться с новым управлением местоположением Android 4.4 (см. https://support.google.com/nexus/answer/3467281). Впоследствии мой код работал по-прежнему.