5
Context context; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Context context = (Permission) this; 
    // In an actual app, you'd want to request a permission when the user 
    // performs an action 
    // that requires that permission. 
    if (Build.VERSION.SDK_INT >= 23) { 
     getPermissionToReadUserContacts(); 
    } 
} 

// Identifier for the permission request 
private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1; 

// Called when the user is performing an action which requires the app to 
// read the 
// user's contacts 
public void getPermissionToReadUserContacts() { 
    // 1) Use the support library version 
    // ContextCompat.checkSelfPermission(...) to avoid 
    // checking the build version since Context.checkSelfPermission(...) is 
    // only available 
    // in Marshmallow 
    // 2) Always check for permission (even if permission has already been 
    // granted) 
    // since the user can revoke permissions at any time through Settings 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.READ_CONTACTS)) { 

      // 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 { 

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

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

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

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
    case READ_CONTACTS_PERMISSIONS_REQUEST: { 
     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(this, "Read Contacts permission granted", 
        Toast.LENGTH_SHORT).show(); 
      // permission was granted, yay! Do the 
      // contacts-related task you need to do. 

     } else { 
      Toast.makeText(this, "Read Contacts permission denied", 
        Toast.LENGTH_SHORT).show(); 
      // permission denied, boo! Disable the 
      // functionality that depends on this permission. 
     } 
     return; 
    } 

    // other 'case' lines to check for other 
    // permissions this app might request 
    } 
} 
// Callback with the request from calling requestPermissions(...) 
/* 
* @Override public void onRequestPermissionsResult(int requestCode, String 
* permissions[], int[] grantResults) { // Make sure it's our original 
* READ_CONTACTS request if (requestCode == 
* READ_CONTACTS_PERMISSIONS_REQUEST) { if (grantResults.length == 1 && 
* grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
* Toast.makeText(this, "Read Contacts permission granted", 
* Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, 
* "Read Contacts permission denied", Toast.LENGTH_SHORT).show(); } } else { 
* super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
* } } 
*/ 

ben title.Any yardım gibi bir hata atıyor projenize dahil olduğunda ben iyi oldu.Ama çalışan bir demo uygulamasında bu çalıştığınızda Takdir edilecektir. Bu bağlamdan dolayı öyle düşünüyorum. ................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .............................................yöntem checkSelfPermission (Bağlam, String) türü için tanımlanmamıştır ContextCompat

+2

kullanmak compile 'com.android.support:appcompat-v7:23.0.1' –

cevap

2

ContextCompat.checkSelfPermission(), 23 veya daha yeni sürümlerde destek kitaplığına gereksinim duyar.

alt düzey build.gradle yılında dependencies blokta ekleyin:

compile 'com.android.support:appcompat-v7:23.1.1' 

burada (muhtemelen var olan) önceki sürümü varsa, şerit değiştirmeniz gerekir tarafımdan temin ile.

+3

Bunu, derleme yapı dosyaları bulunmayan Eclipse ile nasıl çözebilirim? Android SDK Yöneticisi tarafından indirilen appcompat'ın V7 sürümü var mı? Projem için android-support-v7-appcompat.jar dosyasını ekledim. Tutulması eclipse IDE hatası "yöntem checkSelfPermission (MyActivity, String) benim ithalat: ContextCompat türü tarafından undefined olduğunu: import android.support.v7.app. * Import android.support.v7.appcompat. * Import android. support.v4.content.ContextCompat – neuman8

+4

Kendi başarımı çözdüm. Android-support-v4.jar'ın yeni sürümünü de içe aktarmadım. Google, son defa bu yana çok fazla değişiklik yaptı. – neuman8

+0

bana jar Link sağlayabiliyorsunuz, eclipse kullanarak çalışma zamanı entegrasyonunu entegre edebilirim? – Vasant

0

Bu sorun, destek kitaplığının güncel olmaması nedeniyle oluşur. Bu sorunu çözmezse,

Android Tools > Add support library...

muhtemelen:

Eclipse kullanıyorsanız

, ben en basit çözüm projesini sağ tıklatın ve gitmektir buldum ilk SDK'yı güncellemeniz gerekir:

Window > Android SDK Manager > Install Updates

destek kitaplığı eklendi/güncellendi ve artık bu hatayı gösterecektir edilecektir.

İlgili konular