How To Get CurrentLocation

xml files:-

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#fff">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Location"
        android:textSize="15dp"
        android:textColor="#000"
        android:layout_gravity="center"
        android:gravity="center"
        android:textStyle="bold"/>


</LinearLayout>

settingpopup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/redac"
    android:minHeight="150dp"
    android:minWidth="250dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="@drawable/redac"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="2" >

        <TextView
            android:id="@+id/heading"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_weight="2"
            android:gravity="center"
            android:padding="5dp"
            android:text="Location Setting"
            android:textColor="#000"
            android:textSize="18dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#000"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#000"
                android:textSize="16dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/ok"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="@drawable/newbuy"
                android:drawablePadding="10dp"
                android:padding="10dp"
                android:singleLine="true"
                android:text="YES"
                android:textColor="#fff"
                android:textSize="13dp"
                android:textStyle="bold"
                android:visibility="invisible" />

            <Button
                android:id="@+id/close"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="@drawable/newbuy"
                android:drawablePadding="7dp"
                android:padding="7dp"
                android:text="YES"
                android:textColor="#fff"
                android:textSize="13dp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

Java files:-

MyLocation.java

package com.parthivcurrentlocation.android;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

public class MyLocation extends Service implements LocationListener {

protected LocationManager locationManager;
Location location;

private static final long MIN_DISTANCE_FOR_UPDATE = 1;
private static final long MIN_TIME_FOR_UPDATE = 1000;

public MyLocation(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}

public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}


}

LocationMainActivity.java
package com.parthivcurrentlocation.android;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.preference.PreferenceManager;
import android.provider.Settings;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;


public class LocationMainActivity extends Activity {

    TextView location;
    MyLocation appLocationService;
    double latitude,longitude;
    String lati="0.0",longi="0.0";
    Typeface tf;
String fontPath = "fonts/Smoolthan Bold.otf";
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        tf= Typeface.createFromAsset(getAssets(),fontPath);
     
        location=(TextView)findViewById(R.id.textView1);
       
        LocationManager lm = (LocationManager)LocationMainActivity.this.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;

try {
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}

try {
    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}

if(!gps_enabled && !network_enabled)
{

final Dialog viewdialog1  = new Dialog(LocationMainActivity.this);
viewdialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
//viewdialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
viewdialog1.getWindow().setBackgroundDrawable(
       new ColorDrawable(
       android.graphics.Color.TRANSPARENT));
viewdialog1.setContentView(R.layout.settingpopup);
viewdialog1.setCancelable(false);
viewdialog1.setCanceledOnTouchOutside(false);

TextView heading=(TextView)viewdialog1.findViewById(R.id.heading);
heading.setTypeface(tf);



TextView title=(TextView)viewdialog1.findViewById(R.id.title);
heading.setTypeface(tf);
heading.setText("LOCATION SERVICE");

title.setText("Please turn on your location service before entering to the app?");
title.setTypeface(tf);

Button ok=(Button)viewdialog1.findViewById(R.id.ok);
ok.setTypeface(tf);

Button cancel=(Button)viewdialog1.findViewById(R.id.close);
cancel.setTypeface(tf);

cancel.setText("OK");
ok.setText("YES");

ok.setVisibility(View.INVISIBLE);


cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
LocationMainActivity.this.startActivity(intent);

finish();


}
});

ok.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

viewdialog1.dismiss();

Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();


}
});


viewdialog1.show();


}else{
   
appLocationService = new MyLocation(
getApplicationContext());

Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);

try {
latitude = nwLocation.getLatitude();
longitude = nwLocation.getLongitude();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

lati = String.valueOf(latitude);
longi = String.valueOf(longitude);
location.setText(lati+","+longi);


   



    }


}

Screenshots:-










No comments:

Post a Comment

back to top