Android-android run-time permissions for the content provider

Source: Internet
Author: User

---restore content starts---

In the technology of Android data persistence (including file storage, sharedpreferences and SQLite), we found that Google provided us with mode_world_readable and mode_world_ Writeable these two modes are used to give other applications access to the current app's data, but these two modes are deprecated in Android 4.2 version. Why is it? Because Google is no longer recommending this approach for cross-program data sharing, it should use more secure and reliable content provider technology.

1. Introduction to the content provider

Content Provider is primarily used to implement data sharing among different applications, providing a complete set of mechanisms that allow one program to access data from another program, while also guaranteeing the security of the data. Currently, using a content provider is the standard way for Android to share data across programs

Unlike two different global read and write modes in file storage and sharedpreferences storage, the content provider can choose which part of the data to share, thus ensuring that the privacy data in our program is not compromised.

However, before learning the content provider, we need to master another very important knowledge--android running permissions.

2. Run-time permissions

To protect users ' security and privacy as much as possible, Google has introduced runtime permissions in the Android 6.0 version to better protect users ' security and privacy.

(1). Android Permissions Mechanism

In the application development process, some features of the application may involve the security of the system, in order to ensure the security of the system and the normal operation of the application, so Google introduced the concept of application permissions. In fact, the authority of this thing we are not unfamiliar,

The permissions shown in the figure are part of the permissions applied by QQ.

Android permissions, the simple point is that when an application does a certain kind of operation, it must be approved by the system to be allowed to operate. For example, an application that wants to use the network must request permission from the system for network access for normal access.

But this is still not enough, because if an app wants a permission, Then apply in the Androidmanifest.xml file, if it is some dangerous permissions, such as direct text messaging, or direct phone calls and so on permissions, if not through the user's permission to install the application when the default license, this will lead to very serious problems.

Therefore, some permissions must pass the user's consent to the normal authorization, Google in the Android 6.0 began to introduce the runtime permissions, meaning that in the application to use this function, only to apply this permission to the system, and must pass the user's permission to normal use.

(2). Request permission while the program is running

Call_phone This permission is used when making calls, because the phone calls related to the cost of the user's phone, so listed as dangerous rights. In versions prior to Android 6.0, this feature was relatively simple to implement.

1  Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{2     PrivateButton Buttoncall =NULL;3 @Override4     protected voidonCreate (@Nullable Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 Initview ();8     }9     Private voidInitview ()Ten     { OneButtoncall =(Button) Findviewbyid (r.id.id_button_call); AButtoncall.setonclicklistener ( This); -     } -      Public voidOnClick (View v) { the Call (); -     } -     Private voidCall () -     { +Intent Intent =NewIntent (intent.action_call); -Intent.setdata (Uri.parse ("tel:10086")); + startactivity (intent); A     } at  -}

Then request Call_phone permissions in the Androidmanifest.xml file

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3  Package= "Com.example.android_demo">4 5     <uses-permissionAndroid:name= "Android.permission.CALL_PHONE" />6 7     <Application8         Android:allowbackup= "true"9 Android:icon= "@mipmap/ic_launcher"Ten Android:label= "@string/app_name" One Android:roundicon= "@mipmap/ic_launcher_round" A Android:supportsrtl= "true" - Android:theme= "@style/apptheme"> -         <ActivityAndroid:name=". Mainactivity " the Android:launchmode= "Singletask" -             > -             <Intent-filter> -                 <ActionAndroid:name= "Android.intent.action.MAIN"></Action> +                 <categoryAndroid:name= "Android.intent.category.LAUNCHER"></category> -             </Intent-filter> +         </Activity> A     </Application> at </Manifest>

But if this code runs after the Android 6.0 version, there will be a problem.

The reason is because this permission is listed as a dangerous permission by Google, after Android 6.0 can not directly in the Androidmanifest.xml file directly in the application, but to the dynamic application of permissions to the way to normal application

1  Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{2     PrivateButton Buttoncall =NULL;3 @Override4     protected voidonCreate (@Nullable Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 Initview ();8     }9     Private voidInitview ()Ten     { OneButtoncall =(Button) Findviewbyid (r.id.id_button_call); AButtoncall.setonclicklistener ( This); -     } -      Public voidOnClick (View v) { the         //Check Call_phone permission has been authorized, if not, enter the IF statement -         if(Contextcompat.checkselfpermission ( This, Manifest.permission.CALL_PHONE)! =packagemanager.permission_granted) -         { -             //application for Call_phone permissions, 1 indicates the request code +Activitycompat.requestpermissions ( This,NewString[]{manifest.permission.call_phone}, 1); -         } +         Else A         { at Call (); -         } -  -     } -     Private voidCall () -     { inIntent Intent =NewIntent (intent.action_call); -Intent.setdata (Uri.parse ("tel:10086")); to startactivity (intent); +     } -  the     /** * * When we apply for permission, after the user's choice (whether agree or disagree), the system automatically callback this method $      * @paramRequestcode Request CodePanax Notoginseng      * @parampermissions the permission requested -      * @paramgrantresults result of permission request (consent or refusal) the      */ + @Override A      Public voidOnrequestpermissionsresult (intRequestcode, @NonNull string[] permissions, @NonNullint[] grantresults) { the         if(Requestcode = = 1) +         { -             if(Grantresults.length > 0 && grantresults[0] = =packagemanager.permission_granted) $             { $ Call (); -             } -             Else the             { -Toast.maketext ( This, "Permissions not allowed!", Toast.length_short). Show ();Wuyi             } the         } -     } Wu  -}

After the above rewrite, we found that after Android 6.0 version, we can run normally, but Call_phone permissions must be the user's choice

Listed as dangerous rights by Google and have the following permissions

Android-android run-time permissions for the content provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.