2014-12-27 1 views
0

SMS를 통해 내 GPS 좌표를 보내야합니다. SMS 본문에서 위도와 경도 값을 어떻게 가져올 수 있습니까? 무언가를 시도했지만 intent.putExtra ("sms_body", "위도와 경도 값이 있습니다 ..")의 + @ id/lat 또는 + String.valueOf (location.getLongitude()를 사용하여 위와 같이 할 수 없습니다.)) :SMS 본체의 안드로이드를 통해 GPS 좌표를 사용하는 방법

다음은 SmsActivity.java

public class SmsActivity extends MainActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button button1 = (Button) findViewById(R.id.button1); 
     EditText editText = (EditText)findViewById(R.id.telNo); 

     final String telNo = editText.getText().toString(); 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Uri uri = Uri.parse("smsto:" +telNo); 
       Intent intent = new Intent(Intent.ACTION_SENDTO,uri); 
       intent.putExtra("sms_body","there would be latitude and longitude values.."); 
       startActivity(intent); 

       finish(); 
      } 
     });  
    }; 
} 
다음

MainActivity.java가

latitude = (TextView) findViewById(R.id.lat); 
    longitude = (TextView) findViewById(R.id.lon); 
    provText = (TextView) findViewById(R.id.prov); 
    choice = (TextView) findViewById(R.id.choice); 
    fineAcc = (CheckBox) findViewById(R.id.fineAccuracy); 
    choose = (Button) findViewById(R.id.chooseRadio); 

    // Get the location manager 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    // Define the criteria how to select the location provider 
    criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); //default 

    // user defines the criteria 
    choose.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if(fineAcc.isChecked()){ 
       criteria.setAccuracy(Criteria.ACCURACY_FINE); 
       choice.setText("fine accuracy selected"); 
      }else { 
       criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
       choice.setText("coarse accuracy selected"); 
      } 
     } 
    }); 
    criteria.setCostAllowed(false); 
    // get the best provider depending on the criteria 
    provider = locationManager.getBestProvider(criteria, false); 

    // the last known location of this provider 
    Location location = locationManager.getLastKnownLocation(provider); 

    mylistener = new MyLocationListener(); 

    if (location != null) { 
     mylistener.onLocationChanged(location); 
    } else { 
     // leads to the settings because there is no last known location 
//  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
//  startActivity(intent); 
    } 
    // location updates: at least 1 meter and 200millsecs change 
    locationManager.requestLocationUpdates(provider, 200, 1, mylistener); 
} 
private class MyLocationListener implements LocationListener { 

    @Override 
    public void onLocationChanged(Location location) { 
     // Initialize the location fields 
     latitude.setText("Latitude: "+String.valueOf(location.getLatitude())); 
     longitude.setText("Longitude: "+String.valueOf(location.getLongitude())); 

가 어떻게 SMS 본문에 위도와 경도 값을 사용할 수 있습니다입니다?

답변

0

전 내 이전 응용 프로그램

private void sendSMS() { 
    try { 
     if(phonenumber != null && !phonenumber.isEmpty()) { 
      getCurrentLocationFromGPS(); 

      String locstring=latitude+","+longitude; 
      String mapsurl= "http://maps.google.com/maps?q=" + locstring; 

      SmsManager sms = SmsManager.getDefault(); 
      sms.sendTextMessage(phonenumber, null, "I need Help urgent.. I am at this location: " + mapsurl, null, null); 
      Toast.makeText(this, "Help message has been sent", Toast.LENGTH_LONG).show(); 
     } 

    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

에이 코드를 사용하고 못해 문제가 많이 있지만 getCurrentLocationFromGPS() 당신이 너무 위도와 경도 코드를 공유 할 수 있습니다

private void getCurrentLocationFromGPS() { 
    //gps = new GPSTracker(HelpClickActivity.this); 
    if(gps.canGetLocation()){ 
     latitude = gps.getLatitude(); 
     longitude = gps.getLongitude(); 

     dist1=distFrom(latitude, longitude, latitude1, longitude1); 
     dist2=distFrom(latitude, longitude, latitude2, longitude2); 

     Toast.makeText(this, "Dist1: " + dist1 + "kms \nDist2: " + dist2 + "kms", Toast.LENGTH_LONG).show(); 

     calculatemin(dist1,dist2); 
    }else{ 
     gps.showSettingsAlert(); 
    } 
} 
+0

같았다? –

+0

위치 .getlatitude() 및 location.getlongitude(); – SK16

+0

감사합니다. –

관련 문제