看來我的活動沒有正常完成,因爲每次啓動並關閉時都會消耗更多內存。有人可以給我一個想法嗎?這裏是代碼: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();
看來這個線程正在處理UI元素。你不應該那樣做 – nandsito