如何在fusionlocationproviderclient.oncompletelistener()中添加进度条?

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

我正在使用fusedlocationproviderclient查找当前位置,但我希望进度条应显示到找到位置为止,但是我不明白如何在oncomplete()方法中使用进度条这是我的融合定位函数的代码。

progressBar = findViewById(R.id.homrprogressbar);
    progressBar.setIndeterminate(true);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    if (checkPermissions()) {
        if (isLocationEnabled()) {
            mFusedLocationClient.getLastLocation().addOnCompleteListener(
                    new OnCompleteListener<Location>() {
                        @Override
                        public void onComplete(@NonNull Task<Location> task) {
                            progressBar.setVisibility(View.VISIBLE);
                            Location location = task.getResult();
                            if (location == null) {
                                requestNewLocationData();
                            } else {
                                countryname = findcountry(location.getLatitude(), location.getLongitude());
                                if(countryname.equals("India")) {
                                    TypedArray allcountry = getResources().obtainTypedArray(R.array.indianews);
                                    String[] allcountryname = getResources().getStringArray(R.array.indianewsnames);
                                    String[] allcountryurls = getResources().getStringArray(R.array.indiahref);
                                    newsModels = new ArrayList<>();
                                    for (int i = 0; i < allcountry.length(); i++) {
                                        NewsModel newsModel = new NewsModel(allcountry.getResourceId(i, 0), allcountryname[i], allcountryurls[i]);
                                        newsModels.add(newsModel);
                                    }
                                    Singleton.getConstant().addNewsModel(newsModels);
                                    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                                    fragmentTransaction.replace(R.id.frame_layout, homeFragment);
                                    fragmentTransaction.commit();
                                    binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
                                    binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
                                    progressBar.setVisibility(View.GONE);

                                }
                            }
                        }
                    }
            );
        } else {
            Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    } else {
        requestPermissions();
    }
android android-layout android-gps android-fusedlocation
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.