最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解析Android聲明和使用權(quán)限

 更新時(shí)間:2016年11月14日 14:30:38   作者:liuhe688  
這篇文章主要介紹了解析Android聲明和使用權(quán)限,對學(xué)習(xí)android有一定的幫助,有需要的的可以了解一下。

Android定義了一種權(quán)限方案來保護(hù)設(shè)備上的資源和功能。例如,在默認(rèn)情況下,應(yīng)用程序無法訪問聯(lián)系人列表、撥打電話等。下面就以撥打電話為例介紹一下系統(tǒng)對權(quán)限的要求。一般在我們的應(yīng)用中,如果要用到撥打電話的功能,我們會這樣編碼:

Uri uri = Uri.parse("tel:12345678"); 
Intent intent = new Intent(Intent.ACTION_CALL, uri); 
startActivity(intent); 

默認(rèn)情況下,我們無權(quán)訪問撥打電話的Activity,控制臺將會報(bào)以下異常信息:

ERROR/AndroidRuntime: java.lang.SecurityException: Permission Denial:  
starting Intent { act=android.intent.action.CALL dat=tel:12345678 cmp=com.android.phone/.OutgoingCallBroadcaster } 
......  
requires android.permission.CALL_PHONE 

看來,我們是缺少了CALL_PHONE這個(gè)權(quán)限,這個(gè)權(quán)限是Android系統(tǒng)自帶的phone應(yīng)用里定義的權(quán)限:

...... 
<uses-permission android:name="android.permission.CALL_PHONE" /> 
...... 
<activity android:name="OutgoingCallBroadcaster" 
        android:permission="android.permission.CALL_PHONE" 
        android:theme="@android:style/Theme.NoDisplay" 
        android:configChanges="orientation|keyboardHidden"> 
      <!-- CALL action intent filters, for the various ways 
         of initiating an outgoing call. --> 
      <intent-filter> 
        <action android:name="android.intent.action.CALL" /> 
        <category android:name="android.intent.category.DEFAULT" /> 
        <data android:scheme="tel" /> 
      </intent-filter> 
      <intent-filter> 
        <action android:name="android.intent.action.CALL" /> 
        <category 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" /> 
        <data 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> 
</activity> 
...... 

想要使用此功能,必須在我們的AndroidManifest.xml文件中聲明使用此權(quán)限:

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

這告訴系統(tǒng),我們的應(yīng)用使用了此權(quán)限,我們有權(quán)訪問撥打電話的Activity。

我們不僅要問,為什么系統(tǒng)會這樣設(shè)計(jì)呢?答案是為了保護(hù)用戶資源的安全。要想使用此功能,必須在應(yīng)用中聲明權(quán)限信息,這樣一來,在用戶安裝此應(yīng)用時(shí)系統(tǒng)會從應(yīng)用中提取出權(quán)限信息,告訴用戶該應(yīng)用使用到了哪些功能,由用戶判斷該應(yīng)用是否損害自己的安全。

接下來由我來演示一下權(quán)限的定義和使用,我們建立一個(gè)phone項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下:
我們設(shè)計(jì)的流程是在MainActivity中點(diǎn)擊按鈕,然后跳轉(zhuǎn)到PhoneActivity中,我們會為PhoneActiivty定義相應(yīng)的權(quán)限。

我們先看一下MainActivity和PhoneActivity的代碼:
MainActivity.Java如下:

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如下:

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); 
  } 
} 

最重要的是AndroidManifest.xml文件,我們所有的權(quán)限聲明配置都在此文件中完成:

<?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"> 
   
  <!-- 聲明一個(gè)權(quán)限 --> 
  <permission android:protectionLevel="normal"  
        android:name="scott.permission.MY_CALL_PHONE"/> 
         
  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".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> 
    <!-- 為Activity應(yīng)用已定義的權(quán)限 --> 
    <activity android:name=".PhoneActivity"  
         android:permission="scott.permission.MY_CALL_PHONE"> 
      <intent-filter> 
        <!-- 注意這個(gè)action 在其他應(yīng)用中可使用此action訪問此Activity --> 
        <action android:name="scott.intent.action.MY_CALL"/> 
        <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
    </activity> 
  </application> 
  <!-- 在同一應(yīng)用中訪問PhoneActivity也需要加上權(quán)限 --> 
  <uses-permission android:name="scott.permission.MY_CALL_PHONE"/> 
  <uses-sdk android:minSdkVersion="8" /> 
</manifest> 

需要注意的是,在聲明權(quán)限時(shí)需要一個(gè)android:protectionLevel的屬性,它代表“風(fēng)險(xiǎn)級別”。必須是以下值之一:
normal、dangerous、signature、signatureOrSystem。

  • normal表示權(quán)限是低風(fēng)險(xiǎn)的,不會對系統(tǒng)、用戶或其他應(yīng)用程序造成危害。
  • dangerous表示權(quán)限是高風(fēng)險(xiǎn)的,系統(tǒng)將可能要求用戶輸入相關(guān)信息,才會授予此權(quán)限。
  • signature告訴Android,只有當(dāng)應(yīng)用程序所用數(shù)字簽名與聲明此權(quán)限的應(yīng)用程序所有數(shù)字簽名相同時(shí),才能將權(quán)限授給它。
  • signatureOrSystem告訴Android,將權(quán)限授給具有相同數(shù)字簽名的應(yīng)用程序或Android包類,這一級別適用于非常特殊的情況,比如多個(gè)供應(yīng)商需要通過系統(tǒng)影像共享功能時(shí)。

另外一個(gè)是android:permissionGroup屬性,表示一個(gè)權(quán)限組。可以將權(quán)限放在一個(gè)組中,但對于自定義權(quán)限,應(yīng)該避免設(shè)置此屬性。如果確實(shí)希望設(shè)置此屬性,可以使用以下屬性代替:android.permission-group.SYSTEM_TOOLS。

下面是兩個(gè)活動的截圖:

以上過程都是在一個(gè)內(nèi)部完成的,現(xiàn)在假如我們的這個(gè)phone應(yīng)用作為系統(tǒng)內(nèi)置的應(yīng)用,做為開發(fā)者,我們新建一個(gè)app,然后訪問phone應(yīng)用里的PhoneActivity。app的結(jié)構(gòu)圖如下:

我們在MainActivity里放置一個(gè)按鈕,點(diǎn)擊之后跳轉(zhuǎn)到phone應(yīng)用的PhoneActivity中。MainActivity.java代碼如下:

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); 
      } 
    }); 
  } 
} 

然后我們需要在AndroidManifest.xml文件中配置相應(yīng)的權(quán)限:

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

點(diǎn)擊按鈕,就可以順利地跳轉(zhuǎn)到PhoneActivity了。截圖如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

庄浪县| 闵行区| 芮城县| 阜平县| 桓台县| 阿拉善左旗| 大厂| 河津市| 沂南县| 大新县| 邵阳市| 德昌县| 都江堰市| 宾川县| 博客| 建水县| 余庆县| 沂源县| 彩票| 临西县| 尖扎县| 富民县| 静安区| 绥江县| 衡水市| 钟祥市| 宜兰县| 神农架林区| 北海市| 河间市| 苍梧县| 长武县| 时尚| 义马市| 南溪县| 定兴县| 乾安县| 榕江县| 元阳县| 大姚县| 麻江县|