Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的Android应用程序搜索位置花费的时间太长,但没有';最后我不能展示它_Java_Android_Google Maps_Userlocation - Fatal编程技术网

Java 我的Android应用程序搜索位置花费的时间太长,但没有';最后我不能展示它

Java 我的Android应用程序搜索位置花费的时间太长,但没有';最后我不能展示它,java,android,google-maps,userlocation,Java,Android,Google Maps,Userlocation,我在学习用谷歌地图类开发Android应用程序。现在,为了实现it功能,我需要用户的位置数据。现在,到目前为止一切都很好。但每当我在手机上安装应用程序时,应用程序的通知栏都会显示该应用程序正在查找用户的位置,但这需要很长时间。然而,最终,它也不会显示它 有人能帮我吗?我不明白为什么会这样。我还提供了清单文件中的所有权限 为了更清楚,下面是代码: @Override public void onRequestPermissionsResult(int requestCode, @NonNu

我在学习用谷歌地图类开发Android应用程序。现在,为了实现it功能,我需要用户的位置数据。现在,到目前为止一切都很好。但每当我在手机上安装应用程序时,应用程序的通知栏都会显示该应用程序正在查找用户的位置,但这需要很长时间。然而,最终,它也不会显示它

有人能帮我吗?我不明白为什么会这样。我还提供了清单文件中的所有权限

为了更清楚,下面是代码:

@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, 0, 0, locationListener);

            }

        }

    }
除此之外,还有一段代码,我已经使用它来最终确认用户是否确定提供/不提供他们的位置信息:

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

        else {

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        }
谢谢你的帮助

编辑-以下是完整的代码体:

package com.example.userlocation;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

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.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    LocationListener 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, 0, 0, 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) {
                Toast.makeText(MainActivity.this, "User Location" + location.toString(), 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)
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

        else {

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        }


    }
}

你能发布你的完整代码,这样我们就可以从我们这边复制这个问题吗?当然。我已经上传了,谢谢。我刚刚测试了你的代码,toast显示了我的gps坐标,没有任何问题,也没有任何延迟。你看到了什么?没有烤面包吗?你有一个良好的连接和有效的API密钥吗?你能发布你的完整代码,这样我们就可以从我们这边复制这个问题吗?当然。我已经上传了,谢谢。我刚刚测试了你的代码,toast显示了我的gps坐标,没有任何问题,也没有任何延迟。你看到了什么?没有烤面包吗?您是否有良好的连接和有效的API密钥?