开发者社区> 问答> 正文

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

我已经在这个问题上搜寻了几个小时,我感到困惑。我试图简单地使用获取当前的GPS位置,FusedLocationProviderClient并使用当前的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和清单正确设置,如果需要,它们位于下面:

应用程式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>

展开
收起
Puppet 2020-01-07 13:41:11 635 0
1 条回答
写回答
取消 提交回答
  • 确保您具有以下位置权限:

    如何在运行时请求位置许可

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

    
    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();
                    }
                });
    }
    
    2020-01-07 13:41:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载