2013-01-31 58 views
1

如何在SMSManager的文本字符串中指定變量LocationManager? 我想發短信的座標爲短信。如何通過短信發送Google地圖位置

private LocationManager lm; private LocationListener locationListener;

lm = (LocationManager) 
      getSystemService(Context.LOCATION_SERVICE); 

     locationListener = new MyLocationListener(); 

     lm.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 
      0, 
      0, 
      locationListener); 
     lm.requestLocationUpdates(

      LocationManager.NETWORK_PROVIDER, 
      0, 
      0, 
      locationListener); 


btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
    btnSendSMS.setOnClickListener(new View.OnClickListener() { 


     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      sendSMS("55565", "coords"); 

     } 
    }); 



private void sendSMS(String phoneNumber, String message) { 

    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, null, null); 
    } 
+0

請出示你的代碼來獲取位置進行修改。 – Simon

+0

嘿@Simon ..我只是..預先感謝 –

回答

3

,這是我的代碼通過電子郵件發送當前位置和手機短信希望它能幫助:) 根據您的需要

final LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

    final LocationListener mlocListener = new MyLocationListener(); 

    final Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    updateWithNewLocation(location); 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
    mc=mapview.getController(); 
    mc.setZoom(18); 
    mlo=new MyLocationOverlay(this,mapview); 
    mapview.getOverlays().add(mlo); 
    mapview.invalidate(); 
    } 




    public class MyLocationListener implements LocationListener 

    { 

    public void onLocationChanged(final Location location) 
    { 
     updateWithNewLocation(location); 
    } 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

    } 

    private void updateWithNewLocation(final Location location) 
    { 
    mapview=(MapView) findViewById(R.id.map); 
    String latLongString; 
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.text); 
    String addressString = "No address found"; 

    if (location != null) 
    { 
    final double lat = location.getLatitude(); 
    final double lng = location.getLongitude(); 

    latLongString = "Lat:" + lat + "\nLong:" + lng; 
    final double latitude = location.getLatitude(); 
    final double longitude = location.getLongitude(); 
    final Geocoder gc = new Geocoder(this, Locale.getDefault()); 

    final GeoPoint myLocation = new GeoPoint((int)(lat * 1000000), (int)(lng * 1000000)); 
    mapview.getController().animateTo(myLocation); 
    mapview.getController().setCenter(myLocation); 


    try 
    { 
    final List<Address> addresses = gc.getFromLocation(latitude, longitude, 4); 
    final StringBuilder sb = new StringBuilder(); 
    if (addresses.size() > 0) 
    { 
     final Address address = addresses.get(0); 
     for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
     { 
      sb.append(address.getAddressLine(i)).append("\n"); 
     } 
     sb.append(address.getLocality()).append("\n"); 
     sb.append(address.getPostalCode()).append("\n"); 
     sb.append(address.getCountryName()); 
    } 
    addressString = sb.toString(); 
    } 
    catch (final IOException e) 
    { 
    } 
    } 
    else 
    { 
    latLongString = "No location found"; 
    } 
    myLocationText.setText("Your Current Position is:\n" + 
    latLongString + "\n" + addressString); 


    } 





    @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // TODO Auto-generated method stub 
     MenuInflater inf = getMenuInflater(); 
     inf.inflate(R.menu.newmenu, menu); 
      return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     String tv1; 
     TextView myLocationText; 
     myLocationText = (TextView)findViewById(R.id.text); 
     tv1= myLocationText.getText().toString(); 
    switch (item.getItemId()) { 
    case R.id.msg: 

     Intent sendIntent = new Intent(Intent.ACTION_VIEW); 
     sendIntent.putExtra("sms_body", tv1); 
     sendIntent.setType("vnd.android-dir/mms-sms"); 
     startActivity(sendIntent); 

    return(true); 
    case R.id.email: 
     /* Create the Intent */ 
     final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

     /* Fill it with Data */ 
     emailIntent.setType("plain/text"); 
     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject"); 
     emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, tv1); 
     startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
     return(true); 
    } 
    return(super.onOptionsItemSelected(item)); 
    } 
+0

非常好的兄弟,謝謝...利用如何自動發送短信,而不是通過用戶命令? –

+0

http://stackoverflow.com/questions/7620150/can-i-automatically-send-sms-without-the-user-need-to-approve –

+0

檢查此鏈接 –