2017-08-18 6 views
0

私はアンドロイドで初心者です.Marshmallow(NearLocations.execute())でランタイム権限を付与した後、特定の非同期タスクを呼び出そうとしていますが、許可する)私の非同期タスクは複数回呼び出しており、進行状況バーは読み込み中です。この問題を解決するにはどうすればよいですか?Asynctaskランタイム許可で複数回ロードする

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); 
    }  
} 
+0

あなたのAndroidManifestで言及した質問と尋問の許可が出るたびに非同期タスクを実行しています。あなたのタスクを一度だけ実行するか、NearLocationクラスのインスタンスを1つだけ作成して実行してください。 –

+0

なぜあなたはmTask = new Messages()を呼び出すのですか?execute(); OnPostExecute()メソッドは、onPostExecute()メソッドが呼び出されている時間をチェックしましたか –

答えて

0

私はコードを読みましたが、すべてを理解できませんでした。 クラスを非同期タスクのように実行しています。私はそれを好む。

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(); 
} 

}

0
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(); 
    } 
} 

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(); 
     } 
0

あなたのAsyncTask.execute()を呼び出していると、このコードを削除してください:で、forループで

 new NearLocations(lastLocation, v).execute(); 

をあなたのcheckPermission()メソッド:すべての権限w HICHは、すでに付与されます。

 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(); 
     } 
    } 

その後、再びAsyncTaskの複数の呼び出しを引き起こしているonRequestPermissionsResult()、それを呼び出します。

関連する問題