tmV2 Flashcards
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();
}
Sprawdzanie czy ma się uprawnienie
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 jak ma się odpowiednie uprawnienie
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
W CELU WYKORZYSTANIA SENSORÓW TRZEBA DODAĆ
SensorEventListener
Wyświetlenie listy sensorów:
SensorManager menadzerSensorow = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
List listaSensorow =
menadzerSensorow.getSensorList(Sensor.TYPE_ALL);
for (int i=0; i
Kamera, miejsce przechowywania gdzie
powinno być na zewnętrznej pamięci masowej (np. karcie SD):
- DIRECTORY_PICTURES
- DIRECTORY_MUSIC
- DIRECTORY_MOVIES
Environment.getExternalStorageState()
Environment.getExternalStoragePublicDirectory()
Environment.getExternalStorageDirectory()
Context.getExternalFilesDir()
Sprawdzanie czy ma się kamerkę(java)
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
Toast.makeText(getApplicationContext(), “Mam kamere!!!”, Toast.LENGTH_SHORT).show();
}
WebView plik xml i uprawnienie
JavaScript w webView
WebView myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Akcelerometr - manager
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)
Żyroskop - manager
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
Co dodać aby móc korzystać z żyroskopu i akcelerometru (kod kotlin)
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);