[英]Asynctask Loading multiple times in run time permission
I am beginner in android.Here i am trying to call the particular Async-task after Runtime permission granted in Marshmallow ( NearLocations.execute() ) but if i grant the permission(Allow) my Async-tasks are calling multiple times and progress bar is keep on loading.Suggest me how to solve this issue? 我是android的初学者。在这里,我试图在棉花糖( NearLocations.execute() )中授予运行时权限后调用特定的异步任务,但是如果我授予权限(Allow),则我的异步任务会多次调用并显示进度条正在继续加载。建议我如何解决此问题?
public class DashViewScreen extends Fragment implements OnRefreshListener {
public static final int MULTIPLE_PERMISSIONS = 10; // code you want.
String[] permissions = new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION };
Location mLocation;
View v1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.dashboard_layout, container, false);
initialUISetup();
Toast.makeText(getActivity(), "Oncreate", Toast.LENGTH_SHORT).show();
checkPermission(getActivity());
return v;
}
@SuppressWarnings("deprecation")
public void initialUISetup() {
//Async task one
mTask = new Information(v, false);
mTask.execute();
}
private class Information extends AsyncTask<Void, Void, Object> {
private ProgressDialog mProgressDialog;
private View v1;
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getActivity(), "Retrieve Acc", Toast.LENGTH_SHORT).show();
mTask = null;
}
@Override
protected Object doInBackground(Void... params) {
AccountInformation results = null;
results = AppUtil.drex.AccountInformation(v1.getContext());
return results;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
mAccInfo = (AccountInformation) result;
//Async task two
mTask = new Messages().execute();
}
}
private boolean checkPermission(Activity act) {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p : permissions) {
result = ContextCompat.checkSelfPermission(act, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
} else {
if (lastLocation == null) {
locationManager = (LocationManager) v.getContext()
.getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = createListener(v);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
gpsLocationListener);
}
//Async three
new NearLocations(lastLocation, v).execute();
}
}
if (!listPermissionsNeeded.isEmpty()) {
requestPermissions(
listPermissionsNeeded.toArray(new String[listPermissionsNeeded
.size()]), MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
// TODO Auto-generated method stub
switch (requestCode) {
case MULTIPLE_PERMISSIONS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
lastLocation = AppUtil.getMyLastLocation(v.getContext());
if (lastLocation == null) {
locationManager = (LocationManager) v.getContext()
.getSystemService(Context.LOCATION_SERVICE);
lastLocation = AppUtil.getBestLocation(v.getContext(),
locationManager);
gpsLocationListener = createListener(v);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
gpsLocationListener);
}
new NearLocations(lastLocation, v).execute();
} else {
AppUtil.saveBoolToPrefs(getActivity(), "stopPopup", true);
}
return;
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
I read you code but I could not understand every thing. 我读过您的代码,但我听不懂所有内容。 I have a class runs like async task.
我有一个像异步任务一样运行的类。 I prefer use it.
我更喜欢使用它。
public abstract class AsyncTaskSubhi {
Activity activity;
MaterialDialog dialog;
String processName;
public AsyncTaskSubhi(Activity activity, String processName) {
this.activity = activity;
this.processName=processName;
}
public void onPreExecute() {
dialog= new MaterialDialog.Builder(activity)
.title("*****")
.content(processName+"is running").cancelable(false)
.progress(true, 0).autoDismiss(false)
.show();
}
public abstract void doInBackground();
public void execute() {
try {
new Thread(new Runnable() {
@Override
public void run() {
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
onPreExecute();
}
});
doInBackground();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
onPostExecute();
}
});
}
}).start();
}catch (Exception e){
dialog.dismiss();
}
}
public void onPostExecute() {
dialog.dismiss();
new MaterialDialog.Builder(activity)
.title("*****")
.content("OK")
.positiveText(R.string.ok).show();
}
} }
for (String p : permissions) {
result = ContextCompat.checkSelfPermission(act, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
} else {
if (lastLocation == null) {
locationManager = (LocationManager) v.getContext()
.getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = createListener(v);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
gpsLocationListener);
}
//Async three
//it will be executed PackageManager.PERMISSION_GRANTED is true put log where how many time it is executed
new NearLocations(lastLocation, v).execute();
}
}
Try removing this code with 尝试使用删除此代码
for (String p : permissions) {
result = ContextCompat.checkSelfPermission(act, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
} else {
if (lastLocation == null) {
locationManager = (LocationManager) v.getContext()
.getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = createListener(v);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
gpsLocationListener);
}
}
}
if (!listPermissionsNeeded.isEmpty()) {
requestPermissions(
listPermissionsNeeded.toArray(new String[listPermissionsNeeded
.size()]), MULTIPLE_PERMISSIONS);
return false;
}else {
new NearLocations(lastLocation, v).execute();
}
You're calling your AsyncTask.execute(): 您正在调用AsyncTask.execute():
new NearLocations(lastLocation, v).execute();
in a for loop in your checkPermission() method: for every permission which is already granted: 在您的checkPermission()方法的for循环中:对于已经授予的每个权限:
for (String p : permissions) {
result = ContextCompat.checkSelfPermission(act, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
} else {
if (lastLocation == null) {
locationManager = (LocationManager) v.getContext()
.getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = createListener(v);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
gpsLocationListener);
}
//Async three
new NearLocations(lastLocation, v).execute();
}
}
Then again calling it in onRequestPermissionsResult() , which is causing multiple calling of AsyncTask. 然后再次在onRequestPermissionsResult()中对其进行调用,这将导致多次调用AsyncTask。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.