【发布时间】:2017-08-08 18:28:11
【问题描述】:
我的活动似乎没有正确完成,因为每次启动和关闭它时都会消耗更多的内存。有人可以建议我一个想法吗?这是代码:
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 元素。你不应该这样做
标签: android multithreading android-activity memory-leaks