Android 中运行时权限获取联系人信息 Demo
Posted seliote
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 中运行时权限获取联系人信息 Demo相关的知识,希望对你有一定的参考价值。
代码比较简单...
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="club.seliote.readcontact">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="club.seliote.readcontact.MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unknow Data"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package club.seliote.readcontact;
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
Map<String, String> contacts = null;
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 0);
} else {
contacts = readContact();
}
String displayString = "UserName : PhoneNumber
";
if (contacts != null && !contacts.isEmpty()) {
for (Map.Entry<String, String> entry : contacts.entrySet()) {
displayString += entry.getKey() + " : " + entry.getValue() + "
";
}
}
TextView textView = (TextView) this.findViewById(R.id.text_view);
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
textView.setText(displayString);
}
private Map<String, String> readContact() {
Map<String, String> contacts = new HashMap<>();
try (Cursor cursor = MainActivity.this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null)) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String userName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.put(userName, phoneNum);
cursor.moveToNext();
}
} catch (SecurityException exp) {
exp.printStackTrace();
}
return contacts;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 0 :
if (permissions[0] == Manifest.permission.READ_CONTACTS && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
readContact();
} else {
Toast.makeText(MainActivity.this, "Permisson deny!", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
}
以上是关于Android 中运行时权限获取联系人信息 Demo的主要内容,如果未能解决你的问题,请参考以下文章
在android [duplicate]中运行时检查权限时获取NullPointerException