Resolve Android claims and permissions _android

Source: Internet
Author: User

Android defines a privilege scheme to protect resources and functionality on a device. For example, by default, an application cannot access a contact list, make a phone call, and so on. The following is a call to the example of the system to introduce the requirements of the permissions. Generally in our application, if we want to use the function of dialing telephone, we will encode this way:

Uri uri = uri.parse ("tel:12345678"); 
Intent Intent = new Intent (Intent.action_call, URI); 
StartActivity (Intent); 

By default, we do not have access to the activity that calls, and the console will report the following exception information:

Error/androidruntime:java.lang.securityexception:permission denial:  
starting Intent {act= Android.intent.action.CALL dat=tel:12345678 cmp=com.android.phone/. Outgoingcallbroadcaster} ... 
  

It seems that we are missing the Call_phone privilege, which is defined in the phone application that the Android system brings:

... <uses-permission android:name= "Android.permission.CALL_PHONE"/> ... <activity android:name= ". Outgoingcallbroadcaster "android:permission=" Android.permission.CALL_PHONE "Android:theme=" @android: Styl E/theme.nodisplay "android:configchanges=" Orientation|keyboardhidden "> <!--call Action Intent Filte RS, for the various ways of initiating a outgoing call. --> <intent-filter> <action android:name= "Android.intent.action.CALL"/> <categ Ory android:name= "Android.intent.category.DEFAULT"/> <data android:scheme= "Tel"/> </intent-f ilter> <intent-filter> <action android:name= "Android.intent.action.CALL"/> <ca Tegory android:name= "Android.intent.category.DEFAULT"/> <data android:scheme= "Voicemail"/> < /intent-filter> <intent-filter> <action android:name= "andRoid.intent.action.CALL "/> <category android:name=" Android.intent.category.DEFAULT "/> <dat A android:mimetype= "Vnd.android.cursor.item/phone"/> <data android:mimetype= "Vnd.android.cursor.item/phone" _v2 "/> <data android:mimetype= vnd.android.cursor.item/person"/> </intent-filter> </a 
 Ctivity> ...

To use this feature, you must declare the use of this permission in our Androidmanifest.xml file:

<application ...> ... 
</application> 

This tells the system that our application uses this right, and we have access to the activity that calls.

Not only do we ask, why does the system design this way? The answer is to protect the security of user resources. To use this feature, you must declare the permission information in your application, so that when the user installs the application, the system extracts the permission information from the application, telling the user what functions the application uses, and whether the user will be able to determine if the application is compromising its own security.

Next let me demonstrate the definition and use of permissions, we set up a phone project, the project structure is as follows:
The process we designed is to click the button in Mainactivity and then jump to phoneactivity, and we will define the appropriate permissions for phoneactiivty.

Let's take a look at the code for Mainactivity and phoneactivity:
Mainactivity.java is as follows:

Package com.scott.phone; 
 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
 
public class Mainactivity extends activity { 
  @Override public 
  void OnCreate (Bundle savedinstancestate) { 
    Super.oncreate (savedinstancestate); 
    Setcontentview (r.layout.main); 
    Button btn = (button) Findviewbyid (R.ID.BTN); 
    Btn.setonclicklistener (New View.onclicklistener () { 
      @Override public 
      void OnClick (View v) { 
        StartActivity (New Intent (Mainactivity.this, Phoneactivity.class));}} 
 

Phoneactivity.java is as follows:

Package com.scott.phone; 
 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.widget.TextView; 
 
public class Phoneactivity extends activity { 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { 
    super.oncreate (savedinstancestate); 
    TextView TV = new TextView (this); 
    Tv.settext ("yes! It works. "); 
    Setcontentview (TV); 
  } 
} 

The most important is the Androidmanifest.xml file, all of our permission claim configurations are completed in this file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/"  Android "package=" Com.scott.phone "android:versioncode=" 1 "android:versionname=" 1.0 "> <!--declare a permission 
         
  --> <permission android:protectionlevel= "normal" android:name= "Scott.permission.MY_CALL_PHONE"/> <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:na Me= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name = "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> ;/intent-filter> </activity> <!--apply the defined permissions--> <activity android:name= for the activity. Phoneactivity "android:permission=" Scott.permission.MY_CALL_PHONE "> <intent-filter> &L t;! --note that this action can be used by this action in other applications to access this ActIvity--> <action android:name= "Scott.intent.action.MY_CALL"/> <category "android:name=" Id.intent.category.DEFAULT "/> </intent-filter> </activity> </application> <!-- Access to phoneactivity in the same application also requires permission--> <uses-permission android:name= "Scott.permission.MY_CALL_PHONE"/> < 
 USES-SDK android:minsdkversion= "8"/> </manifest>

Note that when declaring permissions, you need a Android:protectionlevel attribute that represents the level of risk. Must be one of the following values:
Normal, dangerous, signature, Signatureorsystem.

    • Normal indicates that permissions are low-risk and do not harm the system, users, or other applications.
    • Dangerous indicates that permissions are risky and the system will likely require users to enter relevant information before granting this permission.
    • Signature tells Android that permissions can be granted only when the application uses a digital signature and all digital signatures for the application that declares the permission.
    • Signatureorsystem tells Android to grant permissions to applications with the same digital signature or to the Android package class, which applies to very specific situations, such as when multiple vendors need to share functionality through system images.

The other is the Android:permissiongroup property, which represents a permission group. You can place permissions in a group, but for custom permissions, you should avoid setting this property. If you do want to set this property, you can use the following properties instead: Android.permission-group.system_tools.

Here is a screenshot of two activities:

The above process is done in one internal, now if our phone application as a system built in application, as a developer, we create a new app, and then access the phone application phoneactivity. The app's chart is as follows:

We put a button in the mainactivity, click and jump to the phone app's phoneactivity. The Mainactivity.java code is as follows:

Package Com.scott.app; 
 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
 
public class Mainactivity extends activity { 
  @Override public 
  void OnCreate (Bundle savedinstancestate) { 
    Super.oncreate (savedinstancestate); 
    Setcontentview (r.layout.main); 
    Button btn = (button) Findviewbyid (R.ID.BTN); 
    Btn.setonclicklistener (New View.onclicklistener () { 
      @Override public 
      void OnClick (View v) { 
        Intent Intent = new Intent ("Scott.intent.action.MY_CALL"); 
        StartActivity (intent); 
      } 
    ); 
   

Then we need to configure the appropriate permissions in the Androidmanifest.xml file:

<application ...> ... 
</application> 
<uses-permission android:name= "Scott.permission.MY_CALL_PHONE"/> 

Click on the button, you can successfully jump to the phoneactivity. Screenshot below:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.