2012-03-09 79 views
-4

我目前正在开始研究我的最后一年项目。我正在做一个名为使用android操作系统的智能车队管理系统的项目。我需要从GPS跟踪器的位置数据喜欢intellitrac X1和显示我的Android应用程序。我很困惑如何开始这个项目。我希望能得到你们的一些指导。谢谢。需要关于android应用程序开发的建议

+0

你想要在你的项目中做什么?请给我一些细节。我曾在Android上使用过LBS。所以请给我一些更多的信息,以便我可以指导你的项目... – Scorpion 2012-03-09 07:01:09

+0

在这个项目中,我需要开发一个运行在android上的车队管理应用程序。该应用程序将从GPS接收器获取位置数据并在谷歌地图中显示。 – user1258686 2012-03-09 07:17:18

+0

所以基本上你想要获取位置(即经度和纬度)的权利? – Scorpion 2012-03-09 08:25:28

回答

0

这是我用于我的应用程序的代码。我用这个来获取用户的当前位置。

public class AndroidLocationActivity {  
     String provider; 
        public double latitude, longitude = 0; 
        CurrentPositionTask getCurrentLocation; 
        Location currentLocation; 
        LocationListener locationListener; 
        LocationManager locationManager; 
        private long time=0; 

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

         try { 
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
          setCriteria(); 
          currentLocation = AndroidLocationActivity.this.locationManager.getLastKnownLocation(provider); 

          if (currentLocation == null) { 
           currentLocation = new Location(provider); 
          } 
          time = currentLocation.getTime(); 

          if (latitude == 0 && longitude == 0) { 
           latitude = currentLocation.getLatitude(); 
           longitude = currentLocation.getLongitude();  
          } 

         } catch (NullPointerException e) { 
          // TODO: handle exception 
          System.out.println("Null"); 
          e.printStackTrace(); 
         } 
         catch (Exception e) { 
          // TODO: handle exception 
          e.printStackTrace(); 
         } 

         runAsyncTask();  
        } 

        @Override 
        protected void onDestroy() { 
         // TODO Auto-generated method stub 
         super.onDestroy(); 
         locationManager.removeUpdates(locationListener); 
        } 

        public void setCriteria() { 
         Criteria criteria = new Criteria(); 
         criteria.setAccuracy(Criteria.ACCURACY_FINE); 
         criteria.setAltitudeRequired(false); 
         criteria.setBearingRequired(false); 
         criteria.setCostAllowed(true); 
         criteria.setPowerRequirement(Criteria.POWER_MEDIUM); 
         provider = locationManager.getBestProvider(criteria, true); 
         if (provider == null) { 
          provider = LocationManager.GPS_PROVIDER; 
         } 
        }  


        public void runAsyncTask() { 
         // TODO Auto-generated method stub 
         if (getCurrentLocation == null) { 
          getCurrentLocation = new CurrentPositionTask();  
         } 

         if (getCurrentLocation != null) { 
          getCurrentLocation.execute("Searching for Location");  
         } 
        } 

        public boolean checkConnection() 
        { 
         //ARE WE CONNECTED TO THE NET 

         ConnectivityManager conMgr = (ConnectivityManager) getSystemService (AndroidLocationActivity.CONNECTIVITY_SERVICE); 
         if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()&& conMgr.getActiveNetworkInfo().isConnected()) { 
          return true; 
         } else { 
          return false; 
         } 
        } 


        private class CurrentPositionTask extends AsyncTask<String, Void, Void> 
        { 
         private ProgressDialog Dialog = new ProgressDialog(AndroidLocationActivity.this); 
         private boolean flag = true; 

         public CurrentPositionTask() { 
          locationListener = new MyLocationListener(); 
         } 

         @Override 
         protected void onPreExecute() { 
          // TODO Auto-generated method stub 
          super.onPreExecute(); 
          try { 
           if (checkConnection()) { 
            Dialog.setTitle("Loading"); 
            Dialog.setMessage("Searching for Location"); 
            Dialog.show(); 
            locationManager.requestLocationUpdates(provider, 0, 0, locationListener); 
           } 
           else { 
            Toast.makeText(getApplicationContext(), "Internet is Not Available", Toast.LENGTH_LONG).show(); 
           }  
          } catch (Exception e) { 
           // TODO: handle exception 
           e.printStackTrace(); 
          } 
         } 

         @Override 
         protected Void doInBackground(String... params) { 
          // TODO Auto-generated method stub 
          while (flag) { 
           if (latitude !=0 && longitude != 0) { 
            flag=false; 
           } 
          } 
          return null; 
         } 

         @Override 
         protected void onPostExecute(Void result) { 
          // TODO Auto-generated method stub 
          super.onPostExecute(result); 
          if (Dialog != null && Dialog.isShowing()) { 
           Dialog.dismiss(); 
           Intent homeIntent = new Intent(AndroidLocationActivity.this.getApplicationContext(), nextactivityname.class); 

从这里您的下一个活动将开始。

homeIntent.putExtra("lat", latitude); 
     homeIntent.putExtra("lng", longitude); 

          startActivity(homeIntent); 
          } 
          locationManager.removeUpdates(locationListener); 
         } 
        } 

        class MyLocationListener implements LocationListener { 

         @Override 
         public void onLocationChanged(Location location) { 
          // TODO Auto-generated method stub 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude();  
          } 
         } 

         @Override 
         public void onProviderDisabled(String arg0) { 
          // TODO Auto-generated method stub 
          Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show(); 
         } 

         @Override 
         public void onProviderEnabled(String arg0) { 
          // TODO Auto-generated method stub 
          Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 
         } 

         @Override 
         public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
          // TODO Auto-generated method stub 
         } 
        } 
    } 

所以这种方式,你可以得到当前的位置与GPS提供在android的帮助。希望这会有所帮助.....

相关问题