2017-03-17 38 views
0

看來我的活動沒有正常完成,因爲每次啓動並關閉時都會消耗更多內存。有人可以給我一個想法嗎?這裏是代碼:Android工作室內存泄漏活動沒有在線程中完成

new Thread(new Runnable() { 
     @Override 
     public void run() { 
      Looper.prepare(); 
      ProgressDialog mProgressDialog = new ProgressDialog(thisActivity); 
      mProgressDialog.setTitle("Getting location"); 
      mProgressDialog.setMessage("Please wait while the device is being located."); 
      mProgressDialog.show(); 
      long startTime = SystemClock.currentThreadTimeMillis(); 
      long time = SystemClock.currentThreadTimeMillis(); 
      while (mLastLocation == null) 
      { 
       if(SystemClock.currentThreadTimeMillis() - startTime >= 5000){ 
        mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
       } 
       if(SystemClock.currentThreadTimeMillis() - time >= 1000) { 
        if (ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
         return; 
        } 
        mLocationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mLocationListener, null); 
        time = SystemClock.currentThreadTimeMillis(); 
       } 
      } 

      String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date()); 

      ServiceModel model = new ServiceModel(0, Globals.LoggedUser.Id, ScheduleAdapter.currentToilet.Id, edt_note.getText().toString(), photo, null, reason, date, mLastLocation.getLatitude(), mLastLocation.getLongitude()); 
      Globals.DomainServiceManager.ServiceRepository.Create(model); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        photo = null; 
        imv_camera.setImageBitmap(null); 
        ActivityScheduleList.setToolbarPosition(0, DATA_NOT_CAPTURED); 
       } 
      }); 
      mProgressDialog.dismiss(); 
      ActivityNotServeced.this.finish(); 
     } 
    }).start(); 
+0

看來這個線程正在處理UI元素。你不應該那樣做 – nandsito

回答

0

首先,Android Studio有一個built in support in finding a memory leak。另一種選擇是使用Leak Canary。現在,在你的代碼中我看到:mLocationManager。我懷疑這會導致泄漏。您可以使用我前面提到的方法進行驗證。當你限制它時,不要傳遞活動上下文 - 使用mActivity.getApplicationContext()

0

有您的ProgressDialog從未取消一些條件,例如

while (mLastLocation == null) 
     { 
      if(SystemClock.currentThreadTimeMillis() - startTime >= 5000){ 
       mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      } 
      if(SystemClock.currentThreadTimeMillis() - time >= 1000) { 
       if (ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        return; 
       } 
       mLocationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mLocationListener, null); 
       time = SystemClock.currentThreadTimeMillis(); 
      } 
     } 

你的線程可能永遠不會結束,但它擁有指向同您的活動(例如在ProgressDialog)。我建議你重構你的代碼。例如,你可以在onStop()方法中取消()你的ProgressDialog。