2016-03-25 30 views
0

Bir Android uygulaması için en son izin sistemini uygulamaya çalışıyorum. Bir düğmeyi tetiklediğimde açılan bir iletişim kutusu var (bu beklenen bir davranıştır) ancak İzin Ver veya Reddet'e dokunulduğumda, uygulama çökmesi ancak izinlerin doğru şekilde verildiği veya reddedildiği anlaşılıyor (dokunma düğmesine bağlı olarak)).Bir izin uygulama çökmesine izin verme veya reddetme android

Bu onClick yöntemidir:

public void okGetItLocation(View view){ 

    Log.d(TAG, "BTN FIRED"); 
    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { 

     Log.d(TAG, "WE HAVE TO SHOW AN EXPLANATION"); 
     // Create a LocationPermissionError fragment 
     NoLocationPermissionsFragment noLocationPermissionsFragment = new NoLocationPermissionsFragment(); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.fragment_container, noLocationPermissionsFragment) 
       .commit(); 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    }else { 

     Log.d(TAG, "POPUP SHOWN"); 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS); 

     // REQUEST_CODE_ASK_PERMISSIONS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

Ve Bu kullanıcının seçim olsun yöntemdir:

@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case REQUEST_CODE_ASK_PERMISSIONS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       Log.d(TAG, "PERMISSIONS GRANTED"); 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       Log.d(TAG, "PERMISSIONS DENIED"); 

       // Create a LocationPermissionError fragment 
       NoLocationPermissionsFragment noLocationPermissionsFragment = new NoLocationPermissionsFragment(); 
       getSupportFragmentManager().beginTransaction() 
         .replace(R.id.fragment_container, noLocationPermissionsFragment) 
         .commit(); 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

Benim günlüğü: olur olanlar için

03-25 12:19:38.673 2944-2944/com.danajeremy.egullapireading D/FIRSTACTIVITY: BTN FIRED 
03-25 12:19:38.673 2944-2944/com.danajeremy.egullapireading D/FIRSTACTIVITY: POPUP SHOWN 
03-25 12:19:39.375 2944-2963/com.danajeremy.egullapireading E/Surface: getSlotFromBufferLocked: unknown buffer: 0xeb16d800 
+1

Logcat'ınızı gönderin. –

+0

Burada, sadece eklediniz –

+1

Bu bir Java yığın izi değil. Uygulamanız çöküyorsa, yığın izi olacaktır. – CommonsWare

cevap

0

Bilmeyi sevmem, çünkü benim Ana Etkinliğime (Manifest'te) NOHISTORY ekledim. Şimdi bir çekicilik gibi çalışır.

+0

Biraz OverKill - Uygulama ama çoğu uygulama ile uyumlu değil: NoHistory Bir etkinliğin geçmiş yığında tutulup tutulmaması gerektiğini belirtin. Bu özellik ayarlanmışsa, kullanıcı etkinlikten ayrılır gitmez işlenecek ve artık geri dönmeyeceklerdir. – Tobliug

İlgili konular