1

我正在开发基于 GPS 的应用程序,该应用程序根据 GPS 提供商获取地址,但有时由于缺乏信号(例如在地下停车场或类似地方)而无法正常工作。所以在这种情况下,我想通过网络提供商获取地址并通过 sendSMS() 方法发送短信。它必须每 10 分钟重复一次,并使用更新的 Location 调用 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);
            }

    }   
4

0 回答 0