-1

但所有其他设备都获得正确的位置,有一个奇怪的行为,谷歌地图和WhatsApp的确切位置在HTC的欲望,但我的应用程序doesn没有地点。请帮助我,这种行为给我造成了很多问题。请尽早帮忙。LocationManager不给我在某些设备上的位置,例如HTC Desire 626或QMobile,

public class GPSTracker extends Service implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = 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; 

    private static final int REQUEST_CODE_PERMISSION_LOCATION = 4; 

    String mPermission = Manifest.permission.ACCESS_COARSE_LOCATION; 

    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 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       // First get location from Network Provider 

       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         if (ActivityCompat.checkSelfPermission((Activity)mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
          // TODO: Consider calling 
          // ActivityCompat#requestPermissions 
          // here to request the missing permissions, and then overriding 
          // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
          //           int[] grantResults) 
          // to handle the case where the user grants the permission. See the documentation 
          // for ActivityCompat#requestPermissions for more details. 
          try { 

            ActivityCompat.requestPermissions((Activity) mContext, new String[]{mPermission}, 
              REQUEST_CODE_PERMISSION_LOCATION); 
            // If any permission above not allowed by user, this condition will execute every time, else your else part will work 

          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
          return null; 
         } 
         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 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(); 
//       } 
//      } 

         if (isNetworkEnabled) { 
          if (location == null) { 
           locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 

           Log.d("Network", "Network"); 
           if (locationManager != null) { 
            location = locationManager 
              .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

            if (location != null) { 
             latitude = location.getLatitude(); 
             longitude = location.getLongitude(); 
            } 
           } 
          } 
         } 
         if (location==null){ 
          try { 
           location = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), true)); 
          } catch(NullPointerException e) {} 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     if (location==null){ 
      location=new Location(""); 
      location.setLatitude(0); 
      location.setLongitude(0); 
      return location; 
     }else 
      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) { 
     latitude=location.getLatitude(); 
     latitude=location.getLongitude(); 
    } 

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

getLocation()总是返回在HTC欲望626模型上的空对象。

Build.gradel(应用程序:一级)

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.bingoit.blaze" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 2 
     versionName "1.1" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile files('libs/ksoap2-android-assembly-3.6.2-jar-with-dependencies.jar') 
    compile 'com.android.support:design:25.3.1' 
    compile 'com.android.support:design:25.3.1' 
    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.android.support:cardview-v7:25.3.1' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    compile 'net.bohush.geometricprogressview:geometricprogressview:1.1.1' 
    compile 'com.android.support:percent:25.3.1' 
    compile 'com.squareup.picasso:picasso:2.4.0' 
    compile 'com.google.android.gms:play-services-maps:10.2.6' 
    testCompile 'junit:junit:4.12' 
    compile 'com.weiwangcn.betterspinner:library-material:1.1.0' 
    compile 'com.github.chrisbanes:PhotoView:2.0.0' 
} 
+0

欢迎StackOverflow上。我强烈建议阅读[我如何问一个好问题?](https://stackoverflow.com/help/how-to-ask),特别是从那里链接的页面 - [如何创建一个最小,完整和可验证示例],然后编辑您的问题以考虑这些页面中的建议。您发布的代码量以及“请尽早提供帮助”的请求不可能鼓励人们帮助您。 – DaveyDaveDave

回答

0

你有没有添加权限APP清单文件?

android.permission.ACCESS_COARSE_LOCATION 

android.permission.ACCESS_FINE_LOCATION 
+0

当然增加了,我可以得到几乎所有的设备除了HTC欲望626和QMobile –

+0

你不觉得这个问题是设备特定的吗?...相当硬件问题! –

相关问题