Android platform obtains the longitude and latitude of the mobile phone's location

Source: Internet
Author: User
Tags sendmsg

The following procedures are tested and have the following functions:

1) obtain the current GPS longitude and latitude Information

2) After other mobile phones send the corresponding text message, the local machine can automatically reply to the text message to obtain the device's latitude and longitude information.

Package com. freshen. test;

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. Location;

Import android. location. LocationListener;

Import android. location. LocationManager;

Import android. OS. Bundle;

Import android. telephony. SmsManager;

Import android. telephony. SmsMessage;

Import android. text. TextUtils;

Import android. text. method. ScrollingMovementMethod;

Import android. util. Log;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

Import android. widget. EditText;

Import android. widget. TextView;

Import android. widget. Toast;

Public class TestLocationActivity extends Activity implements OnClickListener {

TextView txt;

Button getLc;

EditText phoneNumber;

// Locate information

LocationManager locationManager;

Location location;

// Send SMS

Context mContext = null;

// Receives text message broadcasts.

SmsBroadcastReceiver smsbr;

// Longitude and latitude

Double latitude, longpolling;

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

MContext = this;

//

Txt = (TextView) findViewById (R. id. TV _txt );

Txt. setMovementMethod (ScrollingMovementMethod. getInstance ());

GetLc = (Button) findViewById (R. id. bt_getLc );

PhoneNumber = (EditText) findViewById (R. id. phone );

GetLc. setOnClickListener (this );

// Register the SMS to send and receive the broadcast message from the recipient

RegisterReceiver (sendMsg, new IntentFilter ("SENT_SMS_ACTION "));

RegisterReceiver (delivery, new IntentFilter ("DELIVERED_SMS_ACTION "));

// Register and receive SMS broadcasts

IntentFilter smsitf = new IntentFilter ("android. provider. Telephony. SMS_RECEIVED ");

Smsitf. setPriority (10000 );

Smsbr = new SmsBroadcastReceiver ();

RegisterReceiver (smsbr, smsitf );

/* Obtain the longitude and latitude position */

// Location Manager instance

LocationManager = (LocationManager) getSystemService (LOCATION_SERVICE );

// Register the location update listener

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

}

// Listener for location change of LocationListener

Private final LocationListener locationListener = new LocationListener (){

@ Override

Public void onLocationChanged (Location location ){

// TODO Auto-generated method stub

If (location! = Null ){

Latitude = location. getLatitude ();

Longpolling = location. getlongpolling ();

Log. e ("locationListener latitude and longitude", latitude + ":" + longpolling );

}

}

@ Override

Public void onProviderDisabled (String provider ){

// TODO Auto-generated method stub

}

@ Override

Public void onProviderEnabled (String provider ){

// TODO Auto-generated method stub

}

@ Override

Public void onStatusChanged (String provider, int status, Bundle extras ){

// TODO Auto-generated method stub

}};

@ Override

Public void onClick (View arg0 ){

// TODO Auto-generated method stub

If (arg0.getId () = R. id. bt_getLc ){

Log. e ("log", "Start to obtain latitude and longitude information! ");

// Obtain latitude and longitude information

SetTitle ("START obtaining latitude and longitude> ");

String lc = getLoaction ();

// String num = phoneNumber. getEditableText (). toString ();

// Log. e ("phoneNumber", num );

// SendMsg (lc, num );

If (latitude <1) txt. setText ("getting positioning ...... ");

Txt. append (lc );

}

}

// Obtain the longitude and latitude

Public String getLoaction (){

/*

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

Location = locationManager. getLastKnownLocation (LocationManager. NETWORK_PROVIDER );

Int I = 0;

While (location = null ){

Log. e ("log", location + "");

Location = locationManager. getLastKnownLocation (LocationManager. NETWORK_PROVIDER );

If (I ++> 100) break;

}

Double latitude = location. getLatitude ();

Double longpolling = location. getlongpolling ();

Txt. setText ("latitude:" + latitude + "n" + "longitude:" + longpolling );

*/

Return "latitude :(" + latitude + ") t longitude :(" + longpolling + ") n ";

}

// Send SMS

Public void sendMsg (String msg, String num ){

If (TextUtils. isEmpty (msg) | TextUtils. isEmpty (num ))

Return;

SmsManager sms = SmsManager. getDefault ();

Intent sendIntent = new Intent ("SENT_SMS_ACTION ");

PendingIntent sentPI = PendingIntent. getBroadcast (this, 0, sendIntent, 0 );

Intent deliverIntent = new Intent ("DELIVERED_SMS_ACTION ");

PendingIntent deliveryPI = PendingIntent. getBroadcast (this, 0, deliverIntent, 0 );

Sms. sendTextMessage (num, null, msg, sentPI, deliveryPI );

}

// Implement the registered broadcast service

Private BroadcastReceiver sendMsg = new BroadcastReceiver (){

@ Override

Public void onReceive (Context context, Intent intent ){

If (getResultCode () = Activity. RESULT_ OK ){

Toast. makeText (context, "sent successfully! ", Toast. LENGTH_LONG). show ();

} Else {

Toast. makeText (context, "failed to send! ", Toast. LENGTH_LONG). show ();

}

}

};

Private BroadcastReceiver delivery = new BroadcastReceiver (){

@ Override

Public void onReceive (Context context, Intent intent ){

// TODO Auto-generated method stub

Toast. makeText (context, "received! ", Toast. LENGTH_LONG). show ();

}

};

// Cancel registration

@ Override

Protected void onDestroy (){

// TODO Auto-generated method stub

Super. onDestroy ();

UnregisterReceiver (sendMsg );

UnregisterReceiver (delivery );

UnregisterReceiver (smsbr );

}

// SMS listener

Class SmsBroadcastReceiver extends BroadcastReceiver {

@ Override

Public void onReceive (Context context, Intent intent ){

// TODO Auto-generated method stub

Object [] pdus = (Object []) intent. getExtras (). get ("pdus ");

For (Object o: pdus ){

Byte [] data = (byte []) o;

//

SmsMessage sm = SmsMessage. createFromPdu (data );

String sender = sm. getOriginatingAddress ();

String content = sm. getMessageBody ();

// Intercept messages containing gpsl

If (content. contains ("gpsl ")){

This. abortBroadcast ();

SmsManager smg = SmsManager. getDefault ();

Smg. sendTextMessage (sender, null, getLoaction (), null, null );

}

}

}

}

@ Override

Protected void onStop (){

// TODO Auto-generated method stub

Super. onStop ();

LocationManager. removeUpdates (locationListener );

Log. e ("msg", "locate the listener to stop working! ");

}

@ Override

Protected void onRestart (){

// TODO Auto-generated method stub

Super. onRestart ();

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

Log. e ("msg", "locate listener reset! ");

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.