Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.sendlocation;
- import java.io.IOException;
- import java.util.List;
- import com.google.android.maps.MapActivity;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.location.Address;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.telephony.SmsManager;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class SendlocationActivity extends MapActivity implements LocationListener {
- /** Called when the activity is first created. */
- private static final String TAG = "LocationActivity";
- Button btnSendSMS;
- EditText txtPhoneNo;
- // private static final String TAG = "LocationActivity";
- LocationManager locationManager;
- Geocoder geocoder;
- //TextView locationText;
- // MapView map;
- // MapController mapController;
- // GeoPoint point;
- double longitude,latitude;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
- // map = (MapView)this.findViewById(R.id.mapview);
- // map.setBuiltInZoomControls(true);
- // mapController = map.getController();
- // mapController.setZoom(16);
- // locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
- geocoder = new Geocoder(this);
- // Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- // double latitude=80.00;
- // double longitude=34.90;
- if (location != null) {
- Log.d(TAG, location.toString());
- // this.onLocationChanged(location);
- latitude=location.getLatitude();
- longitude=location.getLongitude();
- }
- btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
- txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
- // txtMessage = (EditText) findViewById(R.id.txtMessage);
- btnSendSMS.setOnClickListener(new View.OnClickListener()
- {
- public void onClick(View v)
- {
- String phoneNo = txtPhoneNo.getText().toString();
- String message = Double.toString(latitude) + " " +Double.toString(longitude) ;
- if (phoneNo.length()>0 && message.length()>0)
- sendSMS(phoneNo, message);
- else
- Toast.makeText(getBaseContext(),
- "Please enter both phone number and message.",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void sendSMS(String phoneNumber, String message)
- {
- String SENT = "REQUEST_SENT";
- // PendingIntent pi = PendingIntent.getActivity(this, 0,
- // new Intent(this, SendsmsActivity.class), 0);
- // SmsManager sms = SmsManager.getDefault();
- // sms.sendTextMessage(phoneNumber, null, message, pi, null);
- PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
- new Intent(SENT), 0);
- registerReceiver(new BroadcastReceiver(){
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- switch (getResultCode())
- {
- case Activity.RESULT_OK:
- Toast.makeText(getBaseContext(), "Request sent",
- Toast.LENGTH_SHORT).show();
- break;
- case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
- Toast.makeText(getBaseContext(), "Generic failure",
- Toast.LENGTH_SHORT).show();
- break;
- case SmsManager.RESULT_ERROR_NO_SERVICE:
- Toast.makeText(getBaseContext(), "No service",
- Toast.LENGTH_SHORT).show();
- break;
- case SmsManager.RESULT_ERROR_NULL_PDU:
- Toast.makeText(getBaseContext(), "Null PDU",
- Toast.LENGTH_SHORT).show();
- break;
- case SmsManager.RESULT_ERROR_RADIO_OFF:
- Toast.makeText(getBaseContext(), "Radio off",
- Toast.LENGTH_SHORT).show();
- break;
- }
- }
- }, new IntentFilter(SENT));
- SmsManager sms = SmsManager.getDefault();
- sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
- }
- @Override
- public void onLocationChanged(Location location) {
- // TODO Auto-generated method stub
- // Log.d(TAG, "onLocationChanged with location " + location.toString());
- // String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
- // location.getLongitude(), location.getAltitude(), location.getBearing());
- // this.locationText.setText(text);
- // int latitude = (int)(location.getLatitude() * 1000000);
- // int longitude = (int)(location.getLongitude() * 1000000);
- // double latitude=location.getLatitude();
- // double longitude=location.getLongitude();
- // message=latitude + " " + longitude;
- // mapController.animateTo(point); //<11>
- // mapController.setCenter(point);
- // MapOverlay mapOverlay = new MapOverlay();
- // List<Overlay> listOfOverlays = map.getOverlays();
- // listOfOverlays.clear();
- // listOfOverlays.add(mapOverlay);
- // map.invalidate();
- }
- @Override
- public void onProviderDisabled(String arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onProviderEnabled(String arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
- // TODO Auto-generated method stub
- }
- @Override
- protected boolean isRouteDisplayed() {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- protected void onResume() {
- super.onResume();
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
- }
- @Override
- protected void onPause() {
- super.onPause();
- locationManager.removeUpdates(this); //<8>
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement