Demo:
1)MainActivity.java:
package com.venky.getcurrentlocation;
import
android.app.Activity;
import
android.content.Context;
import
android.location.Location;
import
android.location.LocationListener;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new
LocationListener() {
@Override
public void
onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO
Auto-generated method stub
}
@Override
public void
onProviderEnabled(String arg0) {
// TODO
Auto-generated method stub
}
@Override
public void
onProviderDisabled(String arg0) {
// TODO
Auto-generated method stub
}
@Override
public void
onLocationChanged(Location location) {
// TODO
Auto-generated method stub
double lati =
location.getLatitude();
double longi =
location.getLongitude();
tv.setText("Your
Location\nLatitude=>" + lati + "\nLongitude=>"
+ longi);
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
}
}
2)activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5F04B4"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#ffffff"
/>
</RelativeLayout>
3)Required Permission:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
4)Download this code Click Here
No comments:
Post a Comment