Open GPS in Android like Google MAP with Kotlin

Open GPS in Android like Google MAP with Kotlin
August 6, 2020 1 Comment Android Development,Development Pushpendra Kumar

Hey, Guys welcome to this tutorial. In this complete video, I will make you understand the quick trick for Open GPS in Android like Google MAP with Kotlin. And this is a simple way to achieve our goals. Through this simple video tutorial, you can learn more interesting things about the Open GPS in Android like Google MAP with Kotlin Programming Language. In this video tutorial, you can learn the basics of the turn-on GPS in android programmatically. And you can build a wonderful location listener application in kotlin programming language. And also you can get the answer of how to Open GPS in Android like Google MAP with Kotlin. So watch the complete video. And if you watched the complete video then do not forget to give your suggestion into the comment box. Because your feedback & suggestions do matter for me πŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ‘‡πŸ»

And finally, I would like to say that, If you like this video and it’s helpful for your project then click on the like button and do not forget to subscribe to this YouTube channel for future updates. 

Open GPS in Android

Grate, I hope you watched the complete video. Please find the important code related to this video.

Permissions

Please add the following permissions into your Android Manifest file.

    <!--PERMISSION FOR API SERVICES-->
    <uses-permission android:name="android.permission.INTERNET" />
    
    <!--PERMISSION FOR GPS-->
    <uses-permission android:name="android.permission.ACCESS_GPS" />

    <!--PERMISSIONS FOR LOCATION-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Grate…!!

Now get the code for GPSUtils.kt Class file. This is really very important code for you.

import android.app.Activity
import android.content.Context
import android.location.LocationManager
import android.util.Log
import android.widget.Toast
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.ResolvableApiException
import com.google.android.gms.location.*
import java.lang.Exception

class GPSUtils(context: Context) {
    private val TAG = "GPS"
    private val mContext: Context = context


    private var mSettingClient: SettingsClient? = null
    private var mLocationSettingsRequest: LocationSettingsRequest? = null

    private var mLocationManager: LocationManager? = null
    private var mLocationRequest: LocationRequest? = null


    init {
        mLocationManager = mContext.getSystemService(Context.LOCATION_SERVICE) as? LocationManager

        mSettingClient = LocationServices.getSettingsClient(mContext)

        mLocationRequest = LocationRequest.create()
        mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

        mLocationRequest?.interval = 1000
        mLocationRequest?.fastestInterval = 500


        if (mLocationRequest != null) {
            val builder: LocationSettingsRequest.Builder = LocationSettingsRequest.Builder()
            builder.addLocationRequest(mLocationRequest!!)
            mLocationSettingsRequest = builder.build()
        }
    }

    fun turnOnGPS() {
        if (mLocationManager?.isProviderEnabled(LocationManager.GPS_PROVIDER) == false) {
            mSettingClient?.checkLocationSettings(mLocationSettingsRequest)
                ?.addOnSuccessListener(mContext as Activity) {
                    Log.d(TAG, "turnOnGPS: Already Enabled")
                }
                ?.addOnFailureListener { ex ->
                    if ((ex as ApiException).statusCode
                        == LocationSettingsStatusCodes.RESOLUTION_REQUIRED
                    ) {
                        try {
                            val resolvableApiException = ex as ResolvableApiException
                            resolvableApiException.startResolutionForResult(
                                mContext as Activity,
                                IConstant.DEFAULTS.GPS_CODE
                            )
                        } catch (e: Exception) {
                            Log.d(TAG, "turnOnGPS: Unable to start default functionality of GPS")
                        }

                    } else {
                        if (ex.statusCode == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE) {
                            val errorMessage =
                                "Location settings are inadequate, and cannot be " +
                                        "fixed here. Fix in Settings."
                            Log.e(TAG, errorMessage)
                            Toast.makeText(
                                mContext,
                                errorMessage,
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
                }
        }
    }
}

Create Job. After that get the code for MainActivity.kt File. From where we are calling the above written code.

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class MainActivity : AppCompatActivity() {

    private val TAG = "MAIN_ACTIVITY"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Add The Following line
        GPSUtils(this).turnOnGPS()

    }


    //Integrate the onActivityResult function for the better output
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == IConstant.DEFAULTS.GPS_CODE) {
                Log.d(TAG, "onActivityResult: SUCCESS")
            } else {
                GPSUtils(this).turnOnGPS()
            }
        } else {
            GPSUtils(this).turnOnGPS()
        }
    }
}

Grate Job, After all that, Do not forget to add the permission code shown in the above video. And the above code will be given as below.

    object DEFAULTS {
        const val GPS_CODE = 1002
    }

Grate, I hope you have done with this, And also do not forget to watch the video for the deep understanding.

In Conclusion, You have done with this tutorial. And I hope now the concept of Open GPS in Android with Kotlin Programming Language is clear. Firstly, If you have any doubts regarding the Open GPS in Android. Then you can comment into the comment box. And If you like this tutorial with a complete description of the simple and attractive tutorial in Android then pleaseΒ like my videoΒ on YouTube and do not forget to subscribe to myΒ YouTube channel. Because you will get lots of videos related to swift development with very sort tricks.

In addition, If you are more curious about the development and knowing about tutorials then follow the below links πŸ‘‡πŸ»πŸ‘‡πŸ»πŸ‘‡πŸ»πŸ˜Ž

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1

    Furniture.M106.Com

    Thanks for making the effort to make clear the terminlogy to the newbies!

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *