首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中定义权限,哪种方式更好?

在android中定义权限,哪种方式更好?
EN

Stack Overflow用户
提问于 2013-07-23 18:08:32
回答 1查看 759关注 0票数 1

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

如果我在接收器标签之外指定引导权限,在应用程序标签之外指定引导权限。活动开始。下面是

代码语言:javascript
复制
<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>

如果我在receiver标签中指定权限,我的活动就不会开始。下面是

代码语言:javascript
复制
<?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文档

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-23 18:21:31

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17807189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档