哪个是在android中定义权限的更好方法?

时间:2013-07-23 10:08:32

标签: android broadcastreceiver android-permissions start-activity bootcompleted

我想开始我的活动ON_BOOT_COMPLETED。现在我面临一个奇怪的问题。

如果我在应用程序标记之外指定Receiver标记之外的引导权限。活动开始了。以下

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.broadcaststaticdemo.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>
    <receiver android:name="com.example.broadcaststaticdemo.StartAppOnBoot" >
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.HOME" />
</intent-filter>

    

如果我在接收器标签内指定权限,我的活动就不会开始。 以下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcaststaticdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.broadcaststaticdemo.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>
    <receiver android:name="com.example.broadcaststaticdemo.StartAppOnBoot"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.HOME" />
</intent-filter>

    

虽然我在我的其他应用程序中使用了第二种方法(接收器标签内的权限),但它工作得很好。所以我很困惑在应用程序级别和接收者级别指定权限之间有什么区别。我见过他们提到的android文档

广播公司必须向广播接收方发送消息的权限名称。如果未设置此属性,则元素的权限属性设置的权限适用于广播接收器。如果两个属性均未设置,则接收方不受权限保护。这意味着我们可以指定任何地方。任何帮助都将得到满足

1 个答案:

答案 0 :(得分:2)

当您使用<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />时,它使您的应用程序能够与需要RECEIVE_BOOT_COMPLETE权限的接口进行通信。

但是当您在android:permission中分配属性<receiver>时,您声明与广播接收器接口的任何内容都需要RECEIVE_BOOT_COMPLETE权限。有关它的更多信息,请访问http://developer.android.com/guide/topics/manifest/receiver-element.html#prmsn