2015-08-28 42 views
-2

我正在开发一个应用程序来跟踪车辆并计算他们行驶的距离。我在车上安装了一个设备,通过短信向我的手机发送LatLngs。我的应用程序在SQLiteDatabase中存储包含LatLng的SMS。我的表格每行有一个纬度和经度。我的桌子上的行数日益增长。 当我从表中检索这些数据来绘制地图和计算距离时,我尝试从表中访问数据时,我的应用程序冻结了仅仅一秒钟。如何让它更快?任何建议请。在MainActivity中执行数据库操作是否会冻结UI?

public void calculateDistance(String date) { 
    googleMap.clear(); 
    double cumulativeDistance = 0; 
    String selectLog1="select rowid _id,* from logTable where date='"+date+"' and deviceNo='"+deviceNo+"'"; 
    Cursor cursor4=db.rawQuery(selectLog1,null); 
    if(cursor4.getCount()>2) { 
     while (cursor4.moveToNext()) { 
      String strLatLng = cursor4.getString(cursor4.getColumnIndex("log")); 
      strDate11=cursor4.getString(cursor4.getColumnIndex("date")); 
      String[] strArray = strLatLng.split(","); 
      latitude2 = Double.parseDouble(strArray[0]); 
      longitude2 = Double.parseDouble(strArray[1]); 
      time11=strArray[2]; 
      coordList1.add(new LatLng(latitude2, longitude2)); 
      timeList.add(strLatLng+","+time11); 
     } 

     BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ledorange); 
     for (String strData:timeList){ 
      String[] strDatas=strData.split(","); 
      double latData=Double.parseDouble(strDatas[0]); 
      double lngData=Double.parseDouble(strDatas[1]); 
      String timeAt=strDatas[2]; 
      googleMap.addMarker(new MarkerOptions().position(new LatLng(latData,lngData)).title(vehicleName).icon(bitmapDescriptor).snippet("Known @ " + timeAt + "," + strDate11)); 
     } 

     timeList.removeAll(timeList); 

     CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude2, longitude2), 12.5f); 
     googleMap.animateCamera(cameraUpdate); 
     googleMap.addPolyline(new PolylineOptions().addAll(coordList1).width(10).color(getResources().getColor(R.color.myBlue))); 

     DecimalFormat format = null; 
     for (int i = 0; i < (coordList1.size() - 1); i++) { 
      LatLng l1 = coordList1.get(i); 
      double lat1 = l1.latitude; 
      double lng1 = l1.longitude; 
      Location location1 = new Location(""); 
      location1.setLatitude(lat1); 
      location1.setLongitude(lng1); 

      LatLng l2 = coordList1.get(i + 1); 
      double lat2 = l2.latitude; 
      double lng2 = l2.longitude; 
      Location location2 = new Location(""); 
      location2.setLatitude(lat2); 
      location2.setLongitude(lng2); 

      format = new DecimalFormat("#.##"); 
      double distance = location1.distanceTo(location2)/1000; 
      cumulativeDistance = cumulativeDistance + distance; 
     } 
     if (preferences.contains("errorRatio")) { 
      cumulativeDistance =cumulativeDistance + (cumulativeDistance/100) * preferences.getInt("errorRatio", 0); 
     } 
     tvDistance.setText(format.format(cumulativeDistance) + " Km's"); 
     coordList1.removeAll(coordList1); 
    }else { 
     tvDistance.setText("No Data"); 
    } 
} 
+1

'索引'您的搜索和加入栏是你的朋友。而且'AsyncTask'也是。 –

回答

0

如果您的查询相当大,它可能会冻结您的主线程。

如果我是你,我会使用AsyncTask作为你的读/写操作,因为这会发生在另一个线程上,而不会冻结你的主线程。

0

数据库操作在UI线程上运行,除非另有说明。您可以使用AsyncTask后台线程运行,如下图所示:

private class MyLongTask extends AsyncTask<Void, Void, Void> { 
    protected void doInBackground(Void... params) { 

     // TODO Perform database operations 

     return null; 
    } 

    protected void onPostExecute(Void... params) { 
     // TODO update UI with db results 
    } 
} 

您还可以使用StrictMode检测主线程网络/ IO,以及无论是日志消息或异常通知您。

+0

我可以在我的UI线程中使用'Async Task'吗? –

+1

如果调用.execute(),它将在后台线程上执行主操作,然后在UI线程中调用onPostExecute()。 – fractalwrench

相关问题