我正在尝试运行一个后台服务,该服务将提取最新的 GPS 坐标,然后通过 PHP 脚本将它们发布到服务器。
我有这个工作,因为它将信息发布到服务器,并且它将在后台(作为服务)这样做。
在下面的代码段中,它始终为 false(location == null)。
我是 Java 编程的新手,我认为我做的不正确。我是否应该在有 GPS 更新时更新一个变量,然后从我的后台服务中读取它?
我是否应该将 GPS 功能与这个服务类分开,并在需要数据时调用它?还是我应该使用 AsyncTask ?
抱歉,如果问题很无聊,与 PHP 相比,Java 相当复杂。
谢谢你。
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;
}
}