Get location ( BroadcastReceiver,LocationListener)

2019-07-27 04:57发布

问题:

I am beginner at android. I have a question. I want to when I receive SMS then get my location(latitude and longitude). How can I do. if you have sample code, it will be more helpful.

Thanks for your help.

回答1:

please refer this example. I hope it helps you.

In this example the current location will be display and touched location will also display with lat-lon.



回答2:

For getting latitude and longitude when you have recived an sms on device you will need these steps:

STEP 1: Register an android.provider.Telephony.SMS_RECEIVED BroadcastReceiver for receiving incoming sms notification:

for this you can use following Tutorial :

http://androidsourcecode.blogspot.in/2010/10/receiving-sms-using-broadcastreceiver.html


STEP 2:

for retrieving location on sms received you will need to start an service (IntentService) in onReceive of SMS BroadcastReceiver as:

public class SmsReciever extends BroadcastReceiver {

 private static final String TAG = "Message recieved";

 @Override
 public void onReceive(Context context, Intent intent) {    
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
      // Start your Location IntentService here 
             Intent i = new Intent(context, Location_Intent_Service.class);
            context.startService(i);
    }
 }

}


For How We use IntentService you can see following tutorial:

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/


For how we Use LocationManager from Service class you can follow these Post:

Starting LocationManager as Service Android