android - Warning: Exported activity does not require permis

來源:互聯網
上載者:User

1. 什麼情況下出現:
使用SDK版本為20
建立工程後,添加一個自訂Activity並在manifest.xml中進行定義。
[html] 
<application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity android:name=".MainActivity" 
            android:label="@string/title_activity_main" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
         
        <activity android:name=".SecondActivity" 
            android:exported="false"> 
            <intent-filter> 
                <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
            </intent-filter> 
        </activity> 
    </application> 


添加intent-filter後就會報這個warning。

2. 錯誤原因:
這是因為添加了intent-filter後該Activity已經暴露給了不同進程的應用(其他應用程式),他們執行個體化該Activity不需要任何的許可權。
當然你可以指定此Activity僅用於程式內部使用,或者添加許可權。

3. 解決辦法:
定義Activity的地方添加 android:exported="false" 或者定義許可權。
[html] 
        <activity android:name=".SecondActivity" 
            android:exported="false"> 
            <intent-filter> 
                <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
            </intent-filter> 
        </activity>  www.2cto.com

PS : 由於測試的Activity是為了做添加Activity對應的捷徑到HomeScreen的,所以exported應該為true。要不然無法正常添加。

 android:exported
Whether or not the activity can be launched by components of other applications — "true" if it can be, and "false" if not. If "false", the activity can be launched only by components of the same application or applications with the same user ID.
The default value depends on whether the activity contains intent filters. The absence of any filters means that the activity can be invoked only by specifying its exact class name. This implies that the activity is intended only for application-internal use (since others would not know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the activity is intended for external use, so the default value is "true".
This attribute is not the only way to limit an activity's exposure to other applications. You can also use a permission to limit the external entities that can invoke the activity (see the permissionattribute).

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.