Everything runs very nice,…
Everything runs very nice, but how to make this app to work next time I run it again, after first use? Does it need to close GPS connection after onDestroy() method?
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.razormist.simplegpslocation.MainActivity"> android:id="@+id/btn_locate" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerInParent="true" android:text="Locate"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Latitude:" android:textSize="20sp" android:layout_marginRight="100dp" android:layout_marginEnd="100dp" android:layout_marginBottom="63dp" android:layout_above="@+id/btn_locate" android:layout_alignRight="@+id/btn_locate" android:layout_alignEnd="@+id/btn_locate" /> <TextView android:id="@+id/tv_latitude" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_locate" android:layout_alignStart="@+id/btn_locate" android:layout_alignTop="@+id/textView" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Longhitud:" android:textSize="20sp" android:layout_below="@+id/tv_latitude" android:layout_alignRight="@+id/textView" android:layout_alignEnd="@+id/textView" android:layout_marginTop="16dp" /> <TextView android:id="@+id/tv_longhitud" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_alignLeft="@+id/tv_latitude" android:layout_alignStart="@+id/tv_latitude" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.razormist.simplegpslocation"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:configChanges="orientation" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.razormist.simplegpslocation; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.widget.Toast; /** * Created by Arvin on 4/5/2018. */ public class GPSLocator implements LocationListener { Context context; context = c; } public Location GetLocation(){ if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ Toast.makeText(context, "Permission not granted", Toast.LENGTH_SHORT).show(); return null; } LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if(isGPSEnabled){ locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, this); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); return location; }else{ Toast.makeText(context, "No GPS Detected", Toast.LENGTH_SHORT).show(); } return null; } @Override public void onLocationChanged(Location location) { } @Override } @Override } @Override } }
Button btn_locate; TextView tv_latitude, tv_longhitud;
tv_latitude = (TextView)findViewById(R.id.tv_latitude); tv_longhitud = (TextView)findViewById(R.id.tv_longhitud); @Override GPSLocator gpsLocator = new GPSLocator(getApplicationContext()); Location location = gpsLocator.GetLocation(); if(location != null){ double latitude = location.getLatitude(); double longhitud = location.getLongitude(); } } });
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.