- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 858
<?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.
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 957
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)) } }
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 1466
Sports Tracker
OpenVPN Connect
Remote Desktop 8
Komoot
Shazam
TubeMate
Equalizer + Pro
TeamViewer
VLC
Contacts
Google Calendar
Microsoft Teams
Google Home
Solid Explorer File Manager
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 3580
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");