1

我想开始我的活动 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文档

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

4

1 回答 1

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

于 2013-07-23T10:21:30.840 回答