如何在 fusedlocationproviderclient.oncompletelistener() 中添加进度条?
Posted
技术标签:
【中文标题】如何在 fusedlocationproviderclient.oncompletelistener() 中添加进度条?【英文标题】:How to add progressbar in fusedlocationproviderclient.oncompletelistener()? 【发布时间】:2020-07-20 13:06:06 【问题描述】:我正在使用 fusedlocationproviderclient 来查找当前位置,但我希望进度条应该显示直到找到位置,但我不明白如何在 oncomplete() 方法中使用进度条 这是我的 fusedlocation 函数的代码。
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();
【问题讨论】:
把progressBar.setVisibility(View.VISIBLE);在 if (isLocationEnabled()) 下 【参考方案1】:把progressBar.setVisibility(View.VISIBLE);
放在if (isLocationEnabled())
下面
【讨论】:
以上是关于如何在 fusedlocationproviderclient.oncompletelistener() 中添加进度条?的主要内容,如果未能解决你的问题,请参考以下文章
FusedLocationProvider 始终返回 null Location 对象
FusedLocationProvider 有时会给出错误的位置
使用 fusedLocationProvider.removeLocationUpdates (locationCallback) Android Studio 时出错
FusedLocationProvider 位置更新的单独类或服务?