Introduction
Android Marshmallow is the most recent version for users launched by Google. A wide variety of changes have been done to the Lollipop(5.0 and 5.1) version of Android. One change is called permission model changes, i.e., initially all the app permissions were static, that is at install time, and now in the Marshmallow version of Android it has changed to Run time. In the midst of running application, it requires the user to provide permission to access particular parts and services that app wants to use, for example "Camera permission" while your app wants to use camera and "location access" and reading and "access to the Contacts" and much more.
Types of Permissions
There are two types of permission in android as in the following:
- Normal Permission
- Dangerous Permission
Normal Permissions The Permission which are normal in nature; the app needs to access data or resources outside the app's sandbox that does not access user's private things and there is a very little risk of user information on others appsis called normal permissions.
Examples are: permission to set the time zone , Access Network State, Bluetooth and much more.
Dangerous Permissions The permissions are said to be dangerous in which an app needs to access data that is private to user and there is a risk of user's privacy and could produce the damage to stored data of user in phone and other apps is called dangerous permission.
Examples are : Reading the contacts of user, Access the fine location, camera permission, and much more.
Let's take one of the dangerous permissions to illustrates the permission model in Marshmallow that is
READ_CONTACTS. Let's have a project in which we have take the MainActivity in which we will fetch Contacts of the user.
AndroidManifest.xml First declare permission to access and read user Contacts as shown in code below:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.gkumar.readcontactsapp">
- <uses-permission android:name="android.permission.READ_CONTACTS" />
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- 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>
MainActivity.java All the logic and procedure to fetch stored data means to fetch contacts. In this code I have written code to fetch three fields like name , phone no., and even email. You can see the loops just to find these three fields by making queries via contentResolver object and collecting data in the cursor form.
- package com.example.gkumar.readcontactsapp;
-
- import android.Manifest;
- import android.content.ContentResolver;
- import android.content.DialogInterface;
- import android.content.pm.PackageManager;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Build;
- import android.provider.ContactsContract;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Gravity;
- import android.widget.TextView;
-
- public class MainActivity extends AppCompatActivity {
-
- private TextView contactTextView;
- public static final int REQUEST_READ_CONTACTS = 79;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- contactTextView = (TextView)findViewById(R.id.textView);
-
- if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS)
- == PackageManager.PERMISSION_GRANTED) {
- getContacts();
- } else {
- requestLocationPermission();
- }
-
- }
- protected void requestLocationPermission() {
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
- android.Manifest.permission.READ_CONTACTS)) {
-
-
- } else {
-
- ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_CONTACTS},
- REQUEST_READ_CONTACTS);
- }
-
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode,
- String permissions[], int[] grantResults) {
- switch (requestCode) {
- case REQUEST_READ_CONTACTS: {
-
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
-
- getContacts();
-
- } else {
-
-
-
- }
- return;
- }
-
- }
- }
-
- public void getContacts() {
-
- String phoneNumber = null;
- String email = null;
-
- Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
- String _ID = ContactsContract.Contacts._ID;
- String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
- String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
-
- Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
- String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
- String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
-
- Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
- String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
- String DATA = ContactsContract.CommonDataKinds.Email.DATA;
-
- StringBuffer output = new StringBuffer();
-
- ContentResolver contentResolver = getContentResolver();
-
- Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
-
-
- if (cursor.getCount() > 0) {
-
- while (cursor.moveToNext()) {
-
- String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
- String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
-
- int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
-
- if (hasPhoneNumber > 0) {
-
- output.append("\n First Name:" + name);
-
-
-
- Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
-
- while (phoneCursor.moveToNext()) {
- phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
- output.append("\n Phone number:" + phoneNumber);
-
- }
-
- phoneCursor.close();
-
-
- Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null);
-
- while (emailCursor.moveToNext()) {
-
- email = emailCursor.getString(emailCursor.getColumnIndex(DATA));
-
- output.append("\nEmail:" + email);
-
- }
-
- emailCursor.close();
- }
-
- output.append("\n");
- }
-
- contactTextView.setText(output.toString());
- }
- }
- }
activity_main.xml We have taken only a text view just to set contacts as shown below:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.gkumar.readcontactsapp.MainActivity">
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_margin="10dp"
- android:textSize="16sp"
- android:gravity="center"
- android:text="Contacts Information" />
-
- </RelativeLayout>
Running the Application On Running it in Marshmallow we can see that it is asking for permission to Read the Contacts. If you deny nothing happens and contacts will not be seen.
If you grant permission then it will take a while to fetch and show the contacts to the screen as shown below.
After clicking on allow, that is granting the permission ,now it is fetching contacts it might take a while to get data.
We can see that all the three things like name , phone no., and email have been fetched. It is showing one record because I have only one phone number in my contacts and phone.
Summary
In this article we just see Implementation of Permission Model via fetching and READ_CONTACTS contacts data. We have Implemented the Runtime permission that is dangerous by nature.
Read more articles on Android: