我尝试仅在API 23上从模拟器访问联系人,但无法在运行时检查权限。这是代码
我在Manifest中添加了
<uses-permission android:name="android.permission.READ_CONTACTS"/>
content_main `
<ListView
android:id="@+id/contact_names"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
contact_detail.xml `
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
tools:text="TextView"/>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ListView contactNames;
private static final int REQUEST_CODE_READ_CONTACTS = 1;
private static boolean READ_CONTACTS_GRANTED = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
contactNames = (ListView) findViewById(R.id.contact_names);
int hasReadContactPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
Log.d(TAG, "onCreate: checkSelfPermission "+ hasReadContactPermission);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "fab onClick: starts");
String[] projection={ContactsContract.Contacts.DISPLAY_NAME_PRIMARY};
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
projection,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
if(cursor!=null){
List<String> contacts=new ArrayList<String>();
while (cursor.moveToNext()){
contacts.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
}
cursor.close();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.contact_detail,R.id.name,contacts);
contactNames.setAdapter(adapter);
}
Log.d(TAG, "fab onClick: ends");
}
});
Log.d(TAG, "onCreate: ends");
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult: starts");
switch (requestCode){
case REQUEST_CODE_READ_CONTACTS:{
//if request canclled then ResultArray is Empty
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//permission granted,do the contacts related task you need to do
Log.d(TAG, "onRequestPermissionsResult: permission granted");
READ_CONTACTS_GRANTED = true;
}else {
//permission denied
//disabled the functionalities that depends on this permission
Log.d(TAG, "onRequestPermissionsResult: permission refused");
}
}
}
Log.d(TAG, "onRequestPermissionsResult: ends");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.o`enter code here`nOptionsItemSelected(item);
}
}
每当我点击浮动动作按钮应用程序崩溃时。
答案 0 :(得分:0)
如果您没有权限,则返回false然后请求权限
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS},
1);
然后会显示弹出窗口。
答案 1 :(得分:0)
您需要先使用onClick中的以下示例代码请求权限 -
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.WRITE_CONTACTS},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
insertDummyContact();
您可以从here
了解有关此内容的更多信息答案 2 :(得分:0)
制作两种方法,点击你的工厂查看版本(将此调用放在你的onClickListener中)
if(Build.VERSION.SDK_INT >= 21){
//check for permission
if(!checkPermissionForContact()){
//raise permission dialog
requestPersmissionForContacts();
return;
}
}
两种方法是:
public boolean checkPermissionForContact(){
int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS);
if(result == PackageManager.PERMISSION_GRANTED){
return true;
}else{
return false;
}
}
public void requestPersmissionForContacts(){
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CONTACTS)){
Toast.makeText(MainActivity.this, "Contact Reading Permission needed. Please allow in App Settings for addition functionality.",Toast.LENGTH_LONG).show();
}else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS}, CONTACTS_PERMISSION_REQUEST_CODE);
}
}