为什么这段代码没有显示找到的位置日志

时间:2017-04-21 18:47:19

标签: java android android-studio location

这是我使用本教程编写的代码,但它无法正常工作,我需要找到位置。我对Android编程很新,任何帮助都会受到赞赏。

p.countries

}

这些是日志

public class MainActivity extends Activity implements LocationListener {

LocationManager locationManager;
String provider;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = locationManager.getBestProvider(new Criteria(), false);
    Location location = locationManager.getLastKnownLocation(provider);
    if (ActivityCompat.checkSelfPermission(this, 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.
        return;
    }

    if (location != null) {
        Log.i("location info", "Location achieved");
    } else {
        Log.i("location info", "Location not available :( ");
    }


}

@Override
protected void onResume() {
    super.onResume();

    if (ActivityCompat.checkSelfPermission(this, 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.
        return;
    }
    locationManager.requestLocationUpdates(provider, 400, 10, this);
}

@Override
protected void onPause() {
    super.onPause();

    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    Double lat = location.getLatitude(), lng = location.getLongitude();
    Log.i("location info: Lat",lat.toString());
    Log.i("location info: lng",lng.toString());


}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

这就是假设找到位置的地方。

 04-21 23:35:50.114 1067-2094/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.improfessional.ibtehaj.locationdemo/.MainActivity} from uid 2000 pid 17542 on display 0
04-21 23:35:50.164 1067-2360/? I/ActivityManager: Start proc 17577:com.improfessional.ibtehaj.locationdemo/u0a214 for activity com.improfessional.ibtehaj.locationdemo/.MainActivity
04-21 23:35:50.225 1067-1123/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{43e1802 u0 Starting com.improfessional.ibtehaj.locationdemo}
04-21 23:35:50.243 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.320 17591-17591/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_dependencies_apk.apk@classes.dex: Permission denied
04-21 23:35:50.467 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.507 17602-17602/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_0_apk.apk@classes.dex: Permission denied
04-21 23:35:50.512 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.556 17605-17605/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_1_apk.apk@classes.dex: Permission denied
04-21 23:35:50.561 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.605 17615-17615/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_2_apk.apk@classes.dex: Permission denied
04-21 23:35:50.614 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.653 17617-17617/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_3_apk.apk@classes.dex: Permission denied
04-21 23:35:50.663 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.703 17619-17619/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_4_apk.apk@classes.dex: Permission denied
04-21 23:35:50.707 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.745 17621-17621/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_5_apk.apk@classes.dex: Permission denied
04-21 23:35:50.749 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.787 17624-17624/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_6_apk.apk@classes.dex: Permission denied
04-21 23:35:50.791 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.831 17626-17626/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_7_apk.apk@classes.dex: Permission denied
04-21 23:35:50.836 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.874 17628-17628/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_8_apk.apk@classes.dex: Permission denied
04-21 23:35:50.880 17577-17577/com.improfessional.ibtehaj.locationdemo I/art: override thread count:-j2
04-21 23:35:50.919 17630-17630/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.improfessional.ibtehaj.locationdemo-1@split_lib_slice_9_apk.apk@classes.dex: Permission denied
04-21 23:35:50.924 17577-17577/com.improfessional.ibtehaj.locationdemo W/System: ClassLoader referenced unknown path: /data/app/com.improfessional.ibtehaj.locationdemo-1/lib/arm
04-21 23:35:50.927 17577-17577/com.improfessional.ibtehaj.locationdemo I/InstantRun: starting instant run server: is main process
04-21 23:35:51.047 1067-1078/? D/LocationManagerService: getProviders()=[gps, network]
04-21 23:35:51.047 1067-1078/? D/LocationManagerService: getBestProvider(Criteria[power=NO_REQ acc=---], false)=gps
04-21 23:35:51.048 1067-2372/? D/LocationManagerService: getLastLocation: Request[ACCURACY_FINE gps requested=0 fastest=0 num=1]
04-21 23:35:51.051 17577-17577/com.improfessional.ibtehaj.locationdemo I/location info: Location not available :( 
04-21 23:35:51.053 1067-2374/? D/LocationManagerService: [PowerNavigation] requestLocationUpdates: gps com.improfessional.ibtehaj.locationdemo 400
04-21 23:35:51.053 1067-2374/? D/LocationManagerService: request ab8cbbd gps Request[ACCURACY_FINE gps requested=+400ms fastest=+400ms] from com.improfessional.ibtehaj.locationdemo(10214)
04-21 23:35:51.053 1067-2374/? D/LocationManagerService: provider request: gps ProviderRequest[ON interval=+400ms]
04-21 23:35:51.053 1067-2374/? D/PMS: acquireWL(ff085b2): PARTIAL_WAKE_LOCK  GpsLocationProvider 0x1 1067 1000 null
04-21 23:35:51.053 1067-1112/? D/GpsLocationProvider: [handleMessage] SET_REQUEST
04-21 23:35:51.053 1067-1112/? D/GpsLocationProvider: setRequest ProviderRequest[ON interval=+400ms]
04-21 23:35:51.054 1067-1112/? D/GpsLocationProvider: startNavigating, singleShot is false
04-21 23:35:51.054 1067-1112/? D/GpsLocationProvider: AGPS status: [false], dsable APGS due to no SUPL && C2K settings
04-21 23:35:51.059 1067-1112/? V/GpsLocationProvider: startNavigating: set_agps_qos_time_out complete
04-21 23:35:51.059 1067-1112/? D/GpsLocationProvider: setting position_mode to standalone
04-21 23:35:51.060 1067-1112/? D/PMS: releaseWL(ff085b2): PARTIAL_WAKE_LOCK  GpsLocationProvider 0x1 null
04-21 23:35:51.061 1067-1112/? D/GpsLocationProvider:  write_WLD GPS ON com.improfessional.ibtehaj.locationdemo
04-21 23:35:51.107 17577-17646/com.improfessional.ibtehaj.locationdemo I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3__release_AU (I3fa967cfef)
                                                                                     OpenGL ES Shader Compiler Version: E031.28.00.02
                                                                                     Build Date: 09/29/15 Tue
                                                                                     Local Branch: mybranch14683032
                                                                                     Remote Branch: quic/master
                                                                                     Local Patches: NONE
                                                                                     Reconstruct Branch: NOTHING
04-21 23:35:51.157 1067-1204/? I/ActivityManager: Displayed com.improfessional.ibtehaj.locationdemo/.MainActivity: +1s4ms
04-21 23:35:51.238 1067-1867/? V/GpsLocationProvider: reportStatus status: 3
04-21 23:35:51.238 1067-1867/? V/GpsLocationProvider: reportStatus status: 1
04-21 23:35:51.238 1067-1867/? D/GpsLocationProvider: [GICON] ### GPS_ENABLED_CHANGE_ACTION ###  (true)
04-21 23:35:51.238 14103-14160/? I/IntentController: receive(android.location.GPS_ENABLED_CHANGE,1,false)
04-21 23:35:51.289 14103-14103/? I/LocationControllerInternal: updateLocation(android.location.GPS_ENABLED_CHANGE,true)
04-21 23:35:51.545 1067-1867/? V/GpsLocationProvider: SV count: 26 ephemerisMask: 139ada12 almanacMask: 139ada12

1 个答案:

答案 0 :(得分:0)

  

为什么此代码未显示找到的位置日志

因为您的位置 NULL ,因为您无权从设备访问位置。您必须添加运行时权限检查位置:

 Location location = null;  //Declare globally, not in onCreate

然后在发起onCreate()后在provider中添加以下代码:

 int MyVersion = Build.VERSION.SDK_INT;
        if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            } else {
                location = locationManager.getLastKnownLocation(provider);
            }

如果之前未给出权限,上面的代码将显示一个对话框,询问用户 ALLOW DENY 位置权限。处理允许 DENY 按钮操作onRequestPermissionResult()

 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        location = locationManager.getLastKnownLocation(provider);
                } else {
                    Toast.makeText(getApplicationContext(), ("Please provide access to Location"), Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
  }