milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Small example on how to get gps Location

Details
Written by: Stanko Milosev
Category: Android
Published: 11 April 2022
Last Updated: 13 April 2022
Hits: 1681
  • kotlin
Start empty activity like I already explained here.

In order to display design window, locate activity_main.xml, you will find it under Project -> app -> src -> main -> res -> layout:

Open it and add a button:

In activity_main.xml I have added android:onClick="getGps" so my activity_main.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="getGps"
        tools:layout_editor_absoluteX="166dp"
        tools:layout_editor_absoluteY="441dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
In build.gradle as I already explained here I have added Google Play services com.google.android.gms.location like this:
implementation 'com.google.android.gms:play-services-location:19.0.1'
Now my build.gradle looks like:
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.milosev.getgpslocation"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.gms:play-services-location:19.0.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
In Android -> app -> manifests -> AndroidManifest.xml I added permissions like:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

In order to request permission I In Project -> app -> src -> main -> java -> com -> milosev -> getgpslocation -> MainActivity.kt I wrote:

if (ActivityCompat.checkSelfPermission(
		this,
		Manifest.permission.ACCESS_FINE_LOCATION
	) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
		this,
		Manifest.permission.ACCESS_COARSE_LOCATION
	) != PackageManager.PERMISSION_GRANTED
) {
	if (ActivityCompat.shouldShowRequestPermissionRationale(
			this,
			Manifest.permission.ACCESS_FINE_LOCATION
		)
	) {
		return
	} else {
		// No explanation needed, we can request the permission.
		requestLocationPermission()
	}
}
So my whole MainActivity.kt looks like:
package com.milosev.getgpslocation

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices

class MainActivity : AppCompatActivity() {
    private lateinit var fusedLocationClient: FusedLocationProviderClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    }

    fun getGps(view: View) {
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this,
                    Manifest.permission.ACCESS_FINE_LOCATION
                )
            ) {
                return
            } else {
                // No explanation needed, we can request the permission.
                requestLocationPermission()
            }
        }
        fusedLocationClient.lastLocation
            .addOnSuccessListener { location->
                if (location != null) {
                    // use your location object
                    // get latitude , longitude and other info from this
                }

            }
    }

    private fun requestLocationPermission() {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
            ),
            MY_PERMISSIONS_REQUEST_LOCATION
        )
    }

    companion object {
        private const val MY_PERMISSIONS_REQUEST_LOCATION = 99
        private const val MY_PERMISSIONS_REQUEST_BACKGROUND_LOCATION = 66
    }

}
With piece of code:
fusedLocationClient.lastLocation
.addOnSuccessListener { location->
	if (location != null) {
		// use your location object
		// get latitude , longitude and other info from this
	}
I am actually geting the location.

Create Json With Gson

Details
Written by: Stanko Milosev
Category: Android
Published: 06 March 2022
Last Updated: 12 April 2022
Hits: 1885
  • kotlin
Example how to start new Empty activity, add library to Gradle, and create JSON with GSON.

First start "Empty Activity":

In next step in my case I have choosen language Kotlin, Minimum SDK API 21:

In Android -> Gradle Scripts -> build.gradle:

I have added:

    implementation 'com.google.code.gson:gson:2.9.0'
Now my dependecies look like:
dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.google.code.gson:gson:2.9.0'
}
Go to File -> Sync Project with Gradle Files:

In MainActivity write something like:

internal class Albums {
    var title: String? = null
    var message: String? = null
    var errors = arrayOf<String>()
    var total: String? = null
    var total_pages = 0
    var page = 0
    var limit: String? = null
}
and
val albums = Albums()
albums.title = "Free Music Archive - Albums"
albums.message = ""
albums.total = "11259"
albums.total_pages = 2252
albums.page = 1
albums.limit = "5"

val builder = GsonBuilder()
val gson: Gson = builder.create()
System.out.println(gson.toJson(albums))
My MainActivity now looks like:
package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.gson.Gson
import com.google.gson.GsonBuilder

internal class Albums {
    var title: String? = null
    var message: String? = null
    var errors = arrayOf<String>()
    var total: String? = null
    var total_pages = 0
    var page = 0
    var limit: String? = null
}

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

        val albums = Albums()
        albums.title = "Free Music Archive - Albums"
        albums.message = ""
        albums.total = "11259"
        albums.total_pages = 2252
        albums.page = 1
        albums.limit = "5"

        val builder = GsonBuilder()
        val gson: Gson = builder.create()
        System.out.println(gson.toJson(albums))
    }
}

New and improved my list of best applications

Details
Written by: Stanko Milosev
Category: Android
Published: 04 January 2021
Last Updated: 09 November 2021
Hits: 2118
Here I already gave one list of android application which I was using. Here is new one:

Waze
WhatsApp
Sports Tracker
Facebook
Twitter
OpenVPN Connect
Remote Desktop 8
Komoot
Shazam
TubeMate
Equalizer + Pro
TeamViewer
VLC
Contacts
Google Calendar
Microsoft Teams
Google Home
Solid Explorer File Manager

New line

Details
Written by: Stanko Milosev
Category: Android
Published: 15 April 2016
Last Updated: 15 April 2016
Hits: 4268

Recently I had a problem that I needed to send some text from Android over webview to JavaScript, but with new lines included, problem was that webview didn't respect \r\n.

This is my solution:

...
receivedPacket = new DatagramPacket(buf, buf.length);
...
final String strReceivedPacket = new String(receivedPacket.getData(), 0, receivedPacket.getLength()).replaceAll("\r\n", "\\\\r\\\\n");
  1. Command line building and installing - gradle and adb
  2. Border around controls
  3. AsyncTask
  4. Example of buttons one under another one

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 146 of 165

  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150