【问题标题】:can't get location - entering while loop无法获取位置 - 进入 while 循环
【发布时间】:2018-02-19 18:21:20
【问题描述】:

我还是 android studio 的新手,并试图创建一个只记录我的位置的应用程序,当我运行它时,我没有收到任何错误,但它没有记录位置,而是应用程序被困在一个从一开始就循环,我使用的是我的手机而不是模拟器(带有 Nougat 7.0 的索尼 Xperia XA Ultra),我的 android studio 版本是 2.2.1,这里是代码:

package com.mohamed.locationdemo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // declarations
    LocationManager locationManager; LocationListener locationListener;
    TextView textView;

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


        // initiations
        textView = (TextView) findViewById(R.id.textViewID);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            // when the user change his location
            public void onLocationChanged(Location location) {
                Log.i("Location is: ", location.toString()); // var location here represents the cords of the phone location
                textView.setText(location.toString());
            }

            @Override
            // called when the location service is either enabled or disabled
            public void onStatusChanged(String s, int i, Bundle bundle) {
                textView.setText(s); Log.i("on status changed", "");
            }

            @Override
            // only when the location service enabled
            public void onProviderEnabled(String s) {
                textView.setText(s); Log.i("on provider enabled", "");
            }

            @Override
            // only when the location service is disabled
            public void onProviderDisabled(String s) {
                textView.setText(s); Log.i("on provider disabled", "");
            }
        };


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

        }
        else
        {
            // if we have permission then we will get the location directly
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10, locationListener);
            Log.i("in the else condition", "");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // now we will check if the user answered with yes, then we will get the phone location using LocationManager class
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {Log.i("on request permission", "");
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10, locationListener); Log.i("getting loca in methode", "");
            }
        }
    }
}

XML 清单: XML 和日志: LOG

【问题讨论】:

  • 请写一个最小可行的例子来重现你的问题。另外,请澄清到目前为止您为解决此问题所做的尝试。谢谢。

标签: geolocation location


【解决方案1】:

我可以通过在文件 [build.gradle (module app)] 中修改 google play services 版本来解决这个问题

com.google.android.gms:play-services:11.0.4

成为

com.google.android.gms:play-services:9.0.2

不确定这是否可以在生产应用程序中完成,但为了学习它对我有用,我让我的手机和模拟器工作得很好......

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2012-08-26
    • 2021-03-26
    • 2023-03-14
    相关资源
    最近更新 更多