如何在 fusedlocationproviderclient.oncompletelistener() 中添加进度条?
How to add progressbar in fusedlocationproviderclient.oncompletelistener()?
我正在使用 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())
下
我正在使用 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())
下