Advertisement
code_hacker

Untitled

Oct 27th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 KB | None | 0 0
  1. package com.example.sendlocation;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5.  
  6. import com.google.android.maps.MapActivity;
  7.  
  8.  
  9. import android.app.Activity;
  10. import android.app.PendingIntent;
  11. import android.content.BroadcastReceiver;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.content.IntentFilter;
  15. import android.location.Address;
  16. import android.location.Geocoder;
  17. import android.location.Location;
  18. import android.location.LocationListener;
  19. import android.location.LocationManager;
  20. import android.os.Bundle;
  21. import android.telephony.SmsManager;
  22. import android.util.Log;
  23. import android.view.View;
  24. import android.widget.Button;
  25. import android.widget.EditText;
  26. import android.widget.TextView;
  27. import android.widget.Toast;
  28.  
  29. public class SendlocationActivity extends MapActivity implements LocationListener {
  30.     /** Called when the activity is first created. */
  31.     private static final String TAG = "LocationActivity";
  32.     Button btnSendSMS;
  33.     EditText txtPhoneNo;
  34.   //  private static final String TAG = "LocationActivity";
  35.     LocationManager locationManager;
  36.     Geocoder geocoder;
  37.       //TextView locationText;
  38.      // MapView map;  
  39.      // MapController mapController;
  40.      // GeoPoint point;
  41.      double longitude,latitude;
  42.      
  43.     @Override
  44.     public void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.main);
  47.        
  48.      //   locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
  49.        // map = (MapView)this.findViewById(R.id.mapview);
  50.        // map.setBuiltInZoomControls(true);
  51.        
  52.     //    mapController = map.getController();
  53.       //  mapController.setZoom(16);
  54.        
  55.       //  locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
  56.        
  57.         geocoder = new Geocoder(this);
  58.        
  59.      //   Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  60.        
  61.         locationManager  = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
  62.         Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  63.    //     double latitude=80.00;
  64.      //   double longitude=34.90;
  65.         if (location != null) {
  66.           Log.d(TAG, location.toString());
  67.         //    this.onLocationChanged(location);
  68.               latitude=location.getLatitude();
  69.                longitude=location.getLongitude();
  70.            
  71.           }
  72.        
  73.         btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
  74.         txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
  75. //        txtMessage = (EditText) findViewById(R.id.txtMessage);
  76.  
  77.         btnSendSMS.setOnClickListener(new View.OnClickListener()
  78.         {
  79.             public void onClick(View v)
  80.             {                
  81.                 String phoneNo = txtPhoneNo.getText().toString();
  82.                 String message = Double.toString(latitude) + " " +Double.toString(longitude) ;                
  83.                 if (phoneNo.length()>0 && message.length()>0)                
  84.                     sendSMS(phoneNo, message);                
  85.                 else
  86.                     Toast.makeText(getBaseContext(),
  87.                         "Please enter both phone number and message.",
  88.                         Toast.LENGTH_SHORT).show();
  89.             }
  90.         });        
  91.  
  92.     }
  93.    
  94.    
  95.     private void sendSMS(String phoneNumber, String message)
  96.     {        
  97.          String SENT = "REQUEST_SENT";
  98.  
  99.       //  PendingIntent pi = PendingIntent.getActivity(this, 0,
  100.       //      new Intent(this, SendsmsActivity.class), 0);                
  101.       //  SmsManager sms = SmsManager.getDefault();
  102.       //  sms.sendTextMessage(phoneNumber, null, message, pi, null);
  103.        
  104.         PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
  105.                 new Intent(SENT), 0);
  106.  
  107.         registerReceiver(new BroadcastReceiver(){
  108.             @Override
  109.             public void onReceive(Context arg0, Intent arg1) {
  110.                 switch (getResultCode())
  111.                 {
  112.                     case Activity.RESULT_OK:
  113.                         Toast.makeText(getBaseContext(), "Request sent",
  114.                                 Toast.LENGTH_SHORT).show();
  115.                         break;
  116.                     case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
  117.                         Toast.makeText(getBaseContext(), "Generic failure",
  118.                                 Toast.LENGTH_SHORT).show();
  119.                         break;
  120.                     case SmsManager.RESULT_ERROR_NO_SERVICE:
  121.                         Toast.makeText(getBaseContext(), "No service",
  122.                                 Toast.LENGTH_SHORT).show();
  123.                         break;
  124.                     case SmsManager.RESULT_ERROR_NULL_PDU:
  125.                         Toast.makeText(getBaseContext(), "Null PDU",
  126.                                 Toast.LENGTH_SHORT).show();
  127.                         break;
  128.                     case SmsManager.RESULT_ERROR_RADIO_OFF:
  129.                         Toast.makeText(getBaseContext(), "Radio off",
  130.                                 Toast.LENGTH_SHORT).show();
  131.                         break;
  132.                 }
  133.             }
  134.         }, new IntentFilter(SENT));
  135.        
  136.         SmsManager sms = SmsManager.getDefault();
  137.         sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
  138.  
  139.     }
  140.  
  141.  
  142.     @Override
  143.     public void onLocationChanged(Location location) {
  144.         // TODO Auto-generated method stub
  145.     //   Log.d(TAG, "onLocationChanged with location " + location.toString());
  146.       //    String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
  147.           //              location.getLongitude(), location.getAltitude(), location.getBearing());
  148.         //  this.locationText.setText(text);
  149.          
  150.           // int latitude = (int)(location.getLatitude() * 1000000);
  151.            // int longitude = (int)(location.getLongitude() * 1000000);
  152.      //       double latitude=location.getLatitude();
  153.        //     double longitude=location.getLongitude();
  154.         //    message=latitude + " " + longitude;
  155.            
  156.            
  157.           // mapController.animateTo(point); //<11>
  158.           //  mapController.setCenter(point);
  159.            
  160.       //     MapOverlay mapOverlay = new MapOverlay();
  161.         //   List<Overlay> listOfOverlays = map.getOverlays();
  162.           // listOfOverlays.clear();
  163.           // listOfOverlays.add(mapOverlay);        
  164.  
  165.        
  166.  
  167.            
  168.         //    map.invalidate();
  169.            
  170.          
  171.     }
  172.  
  173.  
  174.     @Override
  175.     public void onProviderDisabled(String arg0) {
  176.         // TODO Auto-generated method stub
  177.        
  178.     }
  179.  
  180.  
  181.     @Override
  182.     public void onProviderEnabled(String arg0) {
  183.         // TODO Auto-generated method stub
  184.        
  185.     }
  186.  
  187.  
  188.     @Override
  189.     public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  190.         // TODO Auto-generated method stub
  191.        
  192.     }
  193.  
  194.  
  195.     @Override
  196.     protected boolean isRouteDisplayed() {
  197.         // TODO Auto-generated method stub
  198.         return false;
  199.     }    
  200.    
  201.        @Override
  202.         protected void onResume() {
  203.           super.onResume();
  204.           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
  205.         }
  206.  
  207.         @Override
  208.         protected void onPause() {
  209.           super.onPause();
  210.           locationManager.removeUpdates(this); //<8>
  211.         }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement