2012-09-27 163 views
1

我正在構建一個簡單的android應用程序,它要求我自動更新位置。自動更新位置不僅會在恢復應用程序時更新,而且在應用程序開啓時必須自動更新。這個應用程序看起來就像是放在車內的GPS系統一樣。它會得到確切的位置並始終更新當前位置。我想在這裏同樣的事情。我在android 2.2上構建這個應用程序。自動更新位置而不重新啓動應用程序

這裏是我的代碼:

package com.example.map; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MapActivity extends Activity implements LocationListener{ 
    private TextView latituteField, longitudeField, accuracyField, altitudeField, bearingField; 
    private LocationManager locationManager; 
    private String provider; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 

    latituteField = (TextView) findViewById(R.id.TextView02); 
    longitudeField = (TextView) findViewById(R.id.TextView04); 
    accuracyField = (TextView) findViewById(R.id.textView1); 
    altitudeField = (TextView) findViewById(R.id.textView2); 
    bearingField = (TextView) findViewById(R.id.textView4); 

    // Get the location manager 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    // Define the criteria how to select the locatioin provider -> use default 
    Criteria criteria = new Criteria(); 
    provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 


    // Initialize the location fields 
    if (location != null) { 
     System.out.println("Provider " + provider + " has been selected."); 
     onLocationChanged(location); 
    } else { 
     latituteField.setText("Location not available"); 
     longitudeField.setText("Location not available"); 
    } 
    } 

    /* Request updates at startup */ 
    @Override 
    protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(provider, 400, 1, this); 
    } 

    /* Remove the locationlistener updates when Activity is paused */ 
    @Override 
    protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    int lat = (int) (location.getLatitude()); 
    int lng = (int) (location.getLongitude()); 
    int acc = (int) (location.getAccuracy()); 
    int alt = (int) (location.getAltitude()); 
    int bearing = (int) (location.getBearing()); 
    latituteField.setText(String.valueOf(lat)); 
    longitudeField.setText(String.valueOf(lng)); 
    accuracyField.setText(String.valueOf(acc)); 
    altitudeField.setText(String.valueOf(alt)); 
    bearingField.setText(String.valueOf(bearing)); 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    Toast.makeText(this, "Enabled new provider " + provider, 
     Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    Toast.makeText(this, "Disabled provider " + provider, 
     Toast.LENGTH_SHORT).show(); 
    } 

這個代碼僅當IM恢復應用程序(我退出,那麼應用程序返回到應用程序)更新位置值。該位置將在視圖上更新。

我也試着去做。喜歡這個。

if(location != null){ 
    do{ 
     System.out.println("Provider " + provider + " has been selected."); 
     onLocationChanged(location); 
     locationManager.requestLocationUpdates(provider, 0, 0, this); 
    }while(location != null); 
    } 
    else{ 
     latituteField.setText("Location not available"); 
     longitudeField.setText("Location not available"); 
    } 

即時將這添加到清單。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
<uses-permission android:name="android.permission.INTERNET" /> 

但是,此代碼會使應用程序在真實手機中崩潰。無論如何要完成這件事?

*注意:我測試了我的真實手機中的所有應用程序並嘗試實現當前位置。 textview會在不暫停或退出應用程序的情況下獲取位置的新值時進行更改。

+0

你在清單中擁有什麼權限? –

+0

@tedHopp看看我的帖子。我已經編輯了我使用的權限的帖子 –

+0

logcat在真實手機上崩潰時說什麼? –

回答

1

你必須把你的位置代碼ScheduledExecutor服務裏面:

http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html

那一個重複現實化功能在後臺例如每30秒或任何時候,你指定。

例子:

scheduleTaskExecutor= Executors.newScheduledThreadPool(5); 

    // This schedule a task to run every 10 seconds: 

    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { 
     public void run() { 


      try {     


       //add your code you would like to be executed every 10 seconds. 



      } catch (IOException e) {       
       e.printStackTrace();       
      }          


     } 
    }, 0, 10, TimeUnit.SECONDS); 

,你可能還需要添加一個權限的粗略位置。

+0

謝謝!有用。現在我需要調整時間來更新每1秒。這不可能嗎?我必須嘗試。希望它有效。再次感謝。 –

+0

不知道是否每秒都會有意義,因爲運行代碼需要一定的時間,並且執行代碼的頻率越高,需要更多的電池。根據你想要對你的應用做什麼,你應該指定最長的時間。 – SunnySonic

相關問題