tmZdanie Flashcards
Uprawnienia do odpalenia aparatu
3 wpisy w AndroidManifest.xml (do androiad 6.0):
Sprawdzenie w kodzie czy ma się uprawnienie do kamery (kotlin)
ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED -> {
// zrób coś, np wyświetl komunikat
Toast.makeText(getApplicationContext(), “Mam kamere!!!”, Toast.LENGTH_SHORT).show();
}
Sprawdzenie czy ma się uprawnienie do kamery i jeśli tak wyświetlenie zdjęcia
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
Toast.makeText(getApplicationContext(), “Mam kamere!!!”, Toast.LENGTH_SHORT).show();
}
Proszenie o uprawnienia
when {
ContextCompat.checkSelfPermission(
CONTEXT,
Manifest.permission.REQUESTED_PERMISSION
) == PackageManager.PERMISSION_GRANTED -> {
// You can use the API that requires the permission.
}
shouldShowRequestPermissionRationale(…) -> {
// In an educational UI, explain to the user why your app requires this
// permission for a specific feature to behave as expected. In this UI,
// include a “cancel” or “no thanks” button that allows the user to
// continue using your app without granting the permission.
showInContextUI(…)
}
else -> {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(
Manifest.permission.REQUESTED_PERMISSION)
}
}
Nadpisanie funkcji gdy poprawnie uzyskano poprawnienie
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { when (requestCode) { PERMISSION_REQUEST_CODE -> { // If request is cancelled, the result arrays are empty. if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { // Permission is granted. Continue the action or workflow // in your app. } else { // Explain to the user that the feature is unavailable because // the features requires a permission that the user has denied. // At the same time, respect the user's decision. Don't link to // system settings in an effort to convince the user to change // their decision. } return }
// Add other 'when' lines to check for other // permissions this app might request. else -> { // Ignore all other requests. } } }
jeżeli chcemy mieć pewność, że sterowanie do nas wróci to można wykorzystać
registerForActivityResult
Kamera - uprawnienia
Sprawdzanie czy ma się kamerkę
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
Toast.makeText(getApplicationContext(), “Mam kamere!!!”, Toast.LENGTH_SHORT).show();
}
Lokalizacja GPS - uprawnienia
ACCESS_FINE_LOCATION - dokładna
ACCESS_COARSE_LOCATION - nie aż tak dokładna
ACCESS_BACKGROUND_LOCATION
WebView - do pliku xml
Akcelerometr
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)
Żyroskop
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
Uprawnienia do sensorów w xml
Mediaplayer odtwarzanie dźwięku
MediaPlayer odtwarzacz = MediaPlayer.create(getApplicationContext(), R.raw.av);
odtwarzacz.start();
Odtwarzanie dźwięku z internetu
String adres = “http://ingeb.org/refer/gaudeamu.MP3”; MediaPlayer odtwarzacz = new MediaPlayer(); odtwarzacz.setAudioStreamType( AudioManager.STREAM_MUSIC); odtwarzacz.setDataSource(adres); odtwarzacz.prepare(); odtwarzacz.start();