我想運行一個後臺服務,將拉動最新的GPS座標,然後通過PHP腳本將它們發佈到服務器。在服務中訪問GPS? Android/Java
我有這個工作,它將信息發佈到服務器,它會在後臺(作爲服務)這樣做。
在下面的代碼片段中,它始終是false(location == null)。
我是Java編程新手,我不認爲我正確地做到了這一點。我是否應該在每次有GPS更新時更新變量,然後從後臺服務中讀取該變量?
我是否應該將GPS功能與此服務類別分開,並在需要數據時調用它?或者我應該使用AsyncTask?
對不起,如果問題沒有問題,Java相對於PHP來說相當複雜。
謝謝。
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
String Long = null;
String Lat = null;
if (location != null) {
Long = String.format("%1$s",location.getLongitude());
Lat = String.format("%1$s", location.getLatitude());
}
else{
Long = "error";
Lat = "error";
}
以下是完整的代碼。
public class UpdaterService extends Service {
private static final String TAG = UpdaterService.class.getSimpleName();
private Updater updater;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
updater = new Updater();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
Log.d(TAG, "onCreate'd");
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
@Override
public synchronized void onStart(Intent intent, int startId) {
// Start the updater
if (!updater.isRunning()) {
updater.start();
}
Log.d(TAG, "onStart'd");
}
@Override
public synchronized void onDestroy() {
super.onDestroy();
// Stop the updater
if (updater.isRunning()) {
updater.interrupt();
}
updater = null;
Log.d(TAG, "onDestroy'd");
}
// ///// Updater Thread
class Updater extends Thread {
private static final long DELAY = 10000; // one minute
private boolean isRunning = false;
public Updater() {
super("Updater");
}
@Override
public void run() {
isRunning = true;
while (isRunning) {
try {
Log.d(TAG, "Updater run'ing");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mydomain.com/save.php");
try {
// Add your data
Log.d(TAG, "Working..");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
String Long = null;
String Lat = null;
if (location != null) {
Long = String.format("%1$s",location.getLongitude());
Lat = String.format("%1$s", location.getLatitude());
}
else{
Long = "error";
Lat = "error";
}
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("lid", "1"));
nameValuePairs.add(new BasicNameValuePair("lat",Lat));
nameValuePairs.add(new BasicNameValuePair("long", Long));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
// Sleep
Thread.sleep(DELAY);
} catch (InterruptedException e) {
// Interrupted
isRunning = false;
}
} // while
}
public boolean isRunning() {
return this.isRunning;
}
}
}
下面是主要活動:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStop() {
super.onStop();
}
// /////// Menu Stuff
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.itemPrefs:
// startService(new Intent(this, UpdaterService.class));
break;
case R.id.itemServiceStart:
startService(new Intent(this, UpdaterService.class));
break;
case R.id.itemServiceStop:
stopService(new Intent(this, UpdaterService.class));
break;
}
return true;
}
}