从GPS_Provider&切换网络_Provider在android中

时间:2013-04-25 07:55:37

标签: android networking gps geocoding

我正在研究基于GPS的应用程序,该应用程序获取基于GPS提供商的地址,但有时由于缺乏信号,如在地下停车场或类似地方,它不起作用。所以在这种情况下我想通过网络提供商获取地址并通过sendSMS()方法发送短信。它必须每10分钟重复一次并使用更新的位置调用sendSMS()方法。

获取GPS位置的代码如下,请您建议我根据需要进行编辑?

    public class WPGActivity extends Activity  {
        ImageButton start;
        Button login;

        String ADDRESS, LOCATION;

        TextView addressText, locationText;
        Location currentLocation;
        double currentLatitude;
        double currentLongitude;

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

            start=(ImageButton)findViewById(R.id.imageButton1);
            login=(Button)findViewById(R.id.button1);

            addressText = (TextView)findViewById(R.id.addressText);
            locationText = (TextView)findViewById(R.id.locationText);

            myLocation();



          //  if(ACTIVE_MODE==1) startApp();

            start.setOnClickListener(new View.OnClickListener() {           
                public void onClick(View v) { 



                getAddress(); // to get address
                sendSMS(); // to send sms
                sendEmail(); // to send email

                }
            });



        }


        public void myLocation(){

            LocationManager locationManager = 
                (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    updateLocation(location);
                }
                public void onStatusChanged(
                        String provider, int status, Bundle extras) {}
                public void onProviderEnabled(String provider) {}
                public void onProviderDisabled(String provider) {}
            };

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 30, locationListener);

        }
          public void getAddress(){
                try{
                    Geocoder gcd = new Geocoder(this, Locale.getDefault());
                    List<Address> addresses = 
                        gcd.getFromLocation(currentLatitude, currentLongitude,100);
                    if (addresses.size() > 0) {
                        StringBuilder result = new StringBuilder();
                        for(int i = 0; i < addresses.size(); i++){
                            Address address =  addresses.get(i);
                            int maxIndex = address.getMaxAddressLineIndex();
                            for (int x = 0; x <= maxIndex; x++ ){
                                result.append(address.getAddressLine(x));
                                result.append(",");
                            }               
                            result.append(address.getLocality());
                            result.append(",");
                            result.append(address.getPostalCode());
                            result.append("\n\n");
                        }
                        ADDRESS = result.toString();
                        addressText.setText(ADDRESS);
                    }
                }
                catch(IOException ex){
                    ADDRESS= ex.getMessage().toString();
                    addressText.setText(ADDRESS);
                }
            }

            void updateLocation(Location location){
                currentLocation = location;
                currentLatitude = currentLocation.getLatitude();
                currentLongitude = currentLocation.getLongitude();
                locationText.setText(LOCATION);
            }

    }   

0 个答案:

没有答案