Android Java App,使用当前位置设置文本字段

问题描述 投票:0回答:1

我已经在这个问题上搜索了好几个小时,我感到困惑。我正在尝试使用FusedLocationProviderClient简单地获取当前GPS位置,并使用当前GPS线设置文本视图。我在主要活动中执行此操作的代码:


GPSPos = LocationServices.getFusedLocationProviderClient(this);
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            TextView textElem = findViewById(R.id.textfield);
            textElem.setText("test2");
            return;
        }
        GPSPos.getLastLocation().addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                TextView textElem = findViewById(R.id.textArea);
                textElem.setText(location.toString());
                textElem.setText("Test");
            }

        });

没有错误,似乎public void onSuccess方法永远不会运行,因为将textview设置为一些纯文本不会更改文本。我需要在模拟器的设置中进行设置吗?

据我所知,我的gradle和清单正确设置,如果需要,它们位于下面:

app gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.gpsmarker"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.gms:play-services:12.0.1'
    implementation 'com.android.support:multidex:1.0.3'
}

清单:


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

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <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>
</manifest>

java android android-location
1个回答
0
投票

确保您具有类似的位置权限:

How to request Location Permission at runtime

之后您可以使用类似的方法:

public static void getLocationUpdates(Context context) {

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);

    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {

            //Use the locationResult to do your work

            if (locationResult == null) {
                return;
            }

        }
    };

    mFusedLocationClient.requestLocationUpdates(getmLocationRequest(),
            mLocationCallback,
            null
    );

    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    if (location != null) {
                        //Use the locationResult to do your work
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    e.fillInStackTrace();
                }
            });
}
© www.soinside.com 2019 - 2024. All rights reserved.