Android:日志不显示设备的当前地理位置

时间:2018-05-20 15:01:48

标签: java android geolocation

一个简单的代码,用于返回日志中的当前位置。 我正在使用LocationManager和LocationListener。似乎没有错误或例外。程序运行,但该位置未显示在日志中

以下是MainActivity代码:

public class MainActivity extends AppCompatActivity {

    private LocationManager locationManager;
    private LocationListener locationListener;

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

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                String longitude = String.valueOf(location.getLongitude());
                String latitude = String.valueOf(location.getLatitude());
                String loc = latitude + ", " + longitude;
                Log.i("Location of phone",loc);
                Toast.makeText(MainActivity.this, loc, Toast.LENGTH_SHORT).show();

            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){

            //Ask for permission
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

        }
        else{

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);

        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
            }
        }
    }

}

logcat的:

05-20 20:26:18.762 30871-30871/? E/Zygote: v2
05-20 20:26:18.762 30871-30871/? I/libpersona: KNOX_SDCARD checking this for 10241
    KNOX_SDCARD not a persona
05-20 20:26:18.763 30871-30871/? E/Zygote: accessInfo : 0
05-20 20:26:18.764 30871-30871/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0010, [-1 -1 -1 -1 0 1]
05-20 20:26:18.766 30871-30871/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.anurag.geolocationdemo 
05-20 20:26:18.772 30871-30871/? I/art: Late-enabling -Xcheck:jni
05-20 20:26:19.223 30871-30871/com.anurag.geolocationdemo I/InstantRun: starting instant run server: is main process
05-20 20:26:19.382 30871-30871/com.anurag.geolocationdemo W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-20 20:26:19.640 30871-30871/com.anurag.geolocationdemo D/TextView: setTypeface with style : 0
05-20 20:26:19.643 30871-30871/com.anurag.geolocationdemo D/TextView: setTypeface with style : 0
05-20 20:26:19.726 30871-30871/com.anurag.geolocationdemo D/TextView: setTypeface with style : 0
05-20 20:26:19.872 30871-30871/com.anurag.geolocationdemo D/Choreographer: init sf_choreo_doframe   debug_Level : 0x4f4cdebug_game_running : false
05-20 20:26:20.080 30871-30871/com.anurag.geolocationdemo D/ViewRootImpl@e453cae[MainActivity]: ThreadedRenderer.create() translucent=false
05-20 20:26:20.108 30871-30871/com.anurag.geolocationdemo D/InputTransport: Input channel constructed: fd=71
05-20 20:26:20.109 30871-30871/com.anurag.geolocationdemo D/ViewRootImpl@e453cae[MainActivity]: setView = DecorView@88c0a4f[MainActivity] touchMode=true
05-20 20:26:20.132 30871-30871/com.anurag.geolocationdemo W/Activity: AppLock checkAppLockState locked:false verifying:false pkgName = com.anurag.geolocationdemo isInMultiWindowMode:false showWhenLocked:false
05-20 20:26:20.134 30871-30871/com.anurag.geolocationdemo D/ViewRootImpl@e453cae[MainActivity]: dispatchAttachedToWindow
05-20 20:26:20.171 30871-30871/com.anurag.geolocationdemo D/ViewRootImpl@e453cae[MainActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][720,1280] result=0x27 surface={isValid=true -307765248} surfaceGenerationChanged=true
    mHardwareRenderer.initialize() mSurface={isValid=true -307765248} hwInitialized=true
05-20 20:26:20.259 30871-30871/com.anurag.geolocationdemo W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-20 20:26:20.282 30871-30871/com.anurag.geolocationdemo D/ViewRootImpl@e453cae[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 720, 1280) ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 0) or=1
05-20 20:26:20.401 30871-30941/com.anurag.geolocationdemo I/OpenGLRenderer: Initialized EGL, version 1.4
05-20 20:26:20.402 30871-30941/com.anurag.geolocationdemo D/OpenGLRenderer: Swap behavior 1
05-20 20:26:20.416 30871-30941/com.anurag.geolocationdemo D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [720x1280]-format:1
05-20 20:26:38.324 30871-30871/com.anurag.geolocationdemo W/Activity: AppLock checkAppLockState locked:false verifying:false pkgName = com.anurag.geolocationdemo isInMultiWindowMode:false showWhenLocked:false
05-20 20:26:38.904 30871-30871/com.anurag.geolocationdemo D/ViewRootImpl@e453cae[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
    mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true -307765248}
05-20 20:26:38.911 30871-30871/com.anurag.geolocationdemo V/InputMethodManager: mNextServedView is change
05-20 20:26:38.923 30871-30871/com.anurag.geolocationdemo V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@81c3386 nm : com.anurag.geolocationdemo ic=null
05-20 20:26:38.923 30871-30871/com.anurag.geolocationdemo I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
05-20 20:26:38.966 30871-30885/com.anurag.geolocationdemo D/InputTransport: Input channel constructed: fd=82
05-20 20:26:38.967 30871-30871/com.anurag.geolocationdemo V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@b58f447 nm : com.anurag.geolocationdemo ic=null
05-20 20:27:07.925 30871-30871/com.anurag.geolocationdemo V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@8acd699 nm : com.anurag.geolocationdemo ic=null
05-20 20:27:07.925 30871-30871/com.anurag.geolocationdemo I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
05-20 20:27:07.929 30871-30871/com.anurag.geolocationdemo D/InputTransport: Input channel constructed: fd=84
    Input channel destroyed: fd=82
05-20 20:27:13.339 30871-30871/com.anurag.geolocationdemo D/ViewRootImpl@e453cae[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.anurag.geolocationdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <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" />

</manifest>

正如我所说,我没有看到任何错误。 我在设备Android 7.0上运行此功能,并且打开了位置和高精度。

如果你能分辨出我的错误,我真的很感激。

0 个答案:

没有答案