Fanidan mustaqil ish mavzu: Telegram kanal yoki guruhni mobil ilova yordamida boshqarish. Bajardi: karimov og`abek


Download 446.09 Kb.
bet9/12
Sana12.03.2023
Hajmi446.09 Kb.
#1263554
1   ...   4   5   6   7   8   9   10   11   12
Bog'liq
Telegram kanal yoki guruhni mobil ilova yordamida boshqarish

Ruxsatlarni tekshiring: Android 6.0 (API darajasi 23) dan boshlab, foydalanuvchi istalgan vaqtda istalgan ilovadan ruxsatlarni bekor qilish huquqiga ega, hatto ilova pastroq API darajasini maqsad qilgan bo'lsa ham. Shunday qilib, xizmatdan foydalanish uchun ilova har safar ruxsatlarni tekshirishi kerak.
Sintaksis: 
if(ContextCompat. checkSelfPermission (thisActivity, Manifest.permission.WRITE_CALENDAR)
!= PackageManager.PERMISSION_GRANTED)
{
// Ruxsat berilmagan
}
Ruxsatlarni so'rash: Yuqoridagi sintaksisdagi checkSelfPermission() usulidan PERMISSION_DENIED qaytarilsa , biz foydalanuvchidan ushbu ruxsatni so'rashimiz kerak. Android ruxsat so'rash uchun bir nechta usullarni taqdim etadi, masalan, requestPermissions() .
Sintaksis: 
ActivityCompat. so'rov ruxsatnomalari ( MainActivity.this ,
permissionarray,
so'rov kodi);


Bu erda permissionArray - bu String tipidagi massiv.
Misol:

  • Kotlin

// Function to check and request permission
public void checkPermission(String permission, int requestCode)
{
// Checking if permission is not granted
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] { permission }, requestCode);
}
else {
Toast.makeText(MainActivity.this, "Permission already granted", Toast.LENGTH_SHORT).show();
}
}

Agar ruxsat allaqachon berilgan bo'lsa , bu funksiya " Tost " xabarini ko'rsatadi, aks holda foydalanuvchidan ruxsat so'raydi.
4-qadam: onRequestPermissionsResult() usulini bekor qilish: onRequestPermissionsResult() foydalanuvchi ruxsat bergan yoki rad etganda chaqiriladi. RequestCode - bu funksiyaning parametrlaridan biri bo'lib, tegishli so'rovlar bo'yicha foydalanuvchi harakatlarini tekshirish uchun ishlatiladi. Bu erda ruxsat va foydalanuvchi harakatini ko'rsatadigan tushdi xabari ko'rsatiladi. 
Misol:

  • Java

  • Kotlin

// This function is called when user accept or decline the permission.
// Request Code is used to check which permission called this function.
// This request code is provided when user is prompt for permission.
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
// Checking whether user granted the permission or not.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Showing the toast message
Toast.makeText(MainActivity.this, "Camera Permission Granted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Camera Permission Denied", Toast.LENGTH_SHORT).show();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Storage Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}


Download 446.09 Kb.

Do'stlaringiz bilan baham:
1   ...   4   5   6   7   8   9   10   11   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling