Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 19 additions & 28 deletions app/src/main/java/com/sudo/androidd20/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.BatteryManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -17,54 +18,44 @@ import com.sudo.androidd20.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
requestLocationPermission()
registerReceiver(this.batteryReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
binding.btLocation.setOnClickListener() {
requestLocation()
}
registerReceiver(this.mBatteryReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
}

private fun requestLocationPermission() {
private val mBatteryReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val power = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
binding.tvBattery.text = power.toString() + "%"
}

}

private fun requestLocation() {
val task = fusedLocationProviderClient.lastLocation
if (ActivityCompat.checkSelfPermission(
this,
android.Manifest.permission.ACCESS_FINE_LOCATION
) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(
this,
android.Manifest.permission.ACCESS_COARSE_LOCATION
) !=
PackageManager.PERMISSION_GRANTED
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
101
1
)
return
}

binding.buttonBasic.setOnClickListener {
task.addOnSuccessListener {
val locationStr = "Latitude: ${it.latitude} Longitude:${it.longitude}"
if (it != null) {
Toast.makeText(this, locationStr, Toast.LENGTH_LONG).show()
binding.tvLocation.text = locationStr
} else
Toast.makeText(this, "No location found", Toast.LENGTH_LONG).show()
}
task.addOnSuccessListener {
val location = "None Gps Location"
binding.tvLocation.text = location
}
}

private val batteryReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val level = intent.getIntExtra("level", 0)
val bat = "$level%"
binding.tvBattery.text = bat
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
tools:context=".MainActivity">

<Button
android:id="@+id/button_basic"
android:id="@+id/btLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn"
android:text="Request permission"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -24,7 +24,7 @@
app:layout_constraintBottom_toBottomOf="@id/tvBattery"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_basic"
app:layout_constraintTop_toBottomOf="@+id/btLocation"
android:textSize="30sp"/>

<TextView
Expand Down