2012-09-21 105 views
1

我得到了這個gps接收方法,它將一些數據存儲到數據庫中。android gps滯後

// GPS 
    private void addGPSListener() { 
     globalconstant.db.setVersion(1); 
     globalconstant.db.setLocale(Locale.getDefault()); 
     globalconstant.db.setLockingEnabled(true); 
    final String gps = 

    "CREATE TABLE IF NOT EXISTS GPS_Values (" 

      + "id INTEGER PRIMARY KEY AUTOINCREMENT, Latitude float(10, 8), Longitude float(10, 8),Accuracy INTEGER,Speed INTEGER,City TEXT,timestamp TIMESTAMP);"; 
    globalconstant.db.execSQL(gps); 


    float f = Float.valueOf(globalconstant.gps_update_value.trim()) 
      .floatValue(); 
    Log.d("FESTIVALE :: ", "Frissítési idő: " 
      + f); 
    float update = f; 


    globalconstant.mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    globalconstant.mlocListener = new MyLocationListener(); 
    globalconstant.mlocManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, (long) update, 0, 
      globalconstant.mlocListener); 
} 

public class MyLocationListener implements LocationListener { 

    public void onLocationChanged(Location loc) { 

     float szel = (float) loc.getLatitude(); 
     float hossz = (float) loc.getLongitude(); 
     int horiAcc = (int) (loc.getAccuracy()); 
     // int speed=(int) ((loc.getSpeed()*3600)/1000); //sebesség km/h-ban 
     int speed = 0; 

     if (loc.hasSpeed()) { 
      speed = (int) ((loc.getSpeed() * 3600)/1000); // sebesség 
                  // km/h-ban 
     } else { 
      speed = 0; 
     } 

     String test = String.format("%.08f", szel); 
     String test2 = String.format("%.08f", hossz); 

     Geocoder geocoder = new Geocoder(main.this, Locale.getDefault()); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(szel, hossz, 
        1); 
      city = addresses.get(0).getLocality(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     ContentValues gps_values = new ContentValues(); 

     gps_values.put("Latitude", test); 
     gps_values.put("Longitude", test2); 
     gps_values.put("Accuracy", horiAcc); 
     gps_values.put("Speed", speed); 
     gps_values.put("City", city); 

     SimpleDateFormat dateFormat = new SimpleDateFormat(
       "yyyy-MM-dd HH:mm:ss"); 
     Date date = new Date(System.currentTimeMillis()); 

     gps_values.put("timestamp", dateFormat.format(date)); 

     try { 
      globalconstant.db.beginTransaction(); 
      globalconstant.db.insert("GPS_Values", null, gps_values); 
      globalconstant.db.setTransactionSuccessful(); 
     } finally { 
      globalconstant.db.endTransaction(); 
     } 

     Log.d("FESTIVALE :: ", "Hely " + test + ", " + test2 + " , " 
       + horiAcc + " , " + speed + " , " + city + "," + dateFormat.format(date)); 
     // String Text = "My current location is: " + "Latitude = " 
     // + loc.getLatitude() + "\nLongitude = " + loc.getLongitude(); 

     // Toast.makeText(getApplicationContext(), "Hely" +test + "\n" + 
     // test2 + "\n" + horiAcc + "\n" +speed + "\n" +city, 
     // Toast.LENGTH_SHORT) 
     // .show(); 

    } 

    public void onProviderDisabled(String provider) { 
     Toast.makeText(getApplicationContext(), "Gps Disabled", 
       Toast.LENGTH_SHORT).show(); 
     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       switch (which) { 
       case DialogInterface.BUTTON_POSITIVE: 
        // show gps otions 
        Intent gpsOptionsIntent = new Intent(
          android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(gpsOptionsIntent); 
        break; 

       case DialogInterface.BUTTON_NEGATIVE: 
        dialog.cancel(); 
        break; 
       } 
      } 
     }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(main.this); 
     builder.setMessage("A GPS nincs aktiválva!\nAktiválja most?") 
       .setPositiveButton("Aktivál", dialogClickListener) 
       .setNegativeButton("Nem", dialogClickListener).show(); 
    } 

    public void onProviderEnabled(String provider) { 
     Toast.makeText(getApplicationContext(), "Gps Enabled", 
       Toast.LENGTH_SHORT).show(); 

    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

}// gps vége 

avarege更新時間爲1秒。但是我的手機可能會滯後(星系s2),因爲有天文鐘。

有沒有人有任何想法,爲什麼?

+0

你是什麼意思是落後? GPS採集,CPU,...? –

+0

the hole apk ...正如我所說的,我可以在天文臺步驟中看到通常1,如果GPS步驟3-5或更多 – David

回答

0

GPS處理器密集程度非常高。你的手機放慢速度並不讓我感到驚訝。這並不罕見。

事實上,如果您將GPS保持足夠長的時間,您的手機會變熱,電池會很快耗盡!

+0

嗯...這意味着我必須將更新時間從1秒 - 可能 - 到2或5? – David

+0

也許吧。不過要小心。我發現使用高功率定期檢查比低功率恆定的GPS使用更好。更好的電池和更好的收購。 –

1

我想我得到了解決:

globalconstant.mlocManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, (long) update, 0, 
       globalconstant.mlocListener); 

的 '更新' 它代表一個 '0' 後。和繼承人什麼,我發現:

requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) 

這意味着最小距離爲「0」,所以這就是爲什麼它是如此「laggy」。 但是,謝謝任何人!