2012-09-23 32 views
0

我想运行一个后台服务,将拉动最新的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; 
      } 



     } 

回答

0

在下面的代码片段始终是假的(location == null)

如果您在仿真器上运行您的代码,您需要手动将GPS数据提供给仿真器(从Eclipse中的DDMS角度)。

我是Java编程新手,我不认为我正确地做到了这一点。我是否应该在每次有GPS更新时更新变量,然后从后台服务中读取该变量?

是的,你可以做到这一点。我早些时候做过类似的事情,它很有用。

我是否应该将GPS功能与此服务类别分开并在需要数据时调用它?或者我应该使用AsyncTask?

不!但是,如果你做这样的事情会是一个不错的主意:

public class UpdaterService extends Service implements LocationListener{ 
} 

至于noob-ish部分。没有问题是愚蠢的,我们都是新手或其他方式。 希望这有助于。