android之permission和user-permission的使用

首先介绍一下permission的参数信息:
  
  
<permission android:description="string resource"
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:permissionGroup="string"
android:protectionLevel=["normal" | "dangerous" |
"signature" | "signatureOrSystem"] />
android:description 对权限的描述,一般两句话,第一句描述这个权限所针对的操作,第二句话告诉用户授予APP这个权限带来的后果
android:icon 图片资源路径
android:label 对权限的一个简短描述
android:name 权限的唯一标识,一般都是使用包名加权限名
android:permissionGroup 权限所属权限组的名称
android:protectionLevel 权限的等级
     normal 最低的等级,声明次权限的app,系统默认授予次权限,不会提示用户
     dangerous 权限对应的操作有安全风险,系统在安装声明此权限的app时会提示用户
      权限声明的操作只针对使用同一个证书签名的app开放
     signatureOrSystem 于 signature  类似,只是增加了rom中自带的app的声明  
注意:android:name属性是必须的,其他的可选,未写的系统会制定默认值
下面写一个系统launcher通过制定一个BroadcastReceiver来创建桌面快捷方式的广播权限来实验
首先在Launcher中的 manifest  这样定义的。(permission中声明权限,然后在receiver中注册这个权限声明,这样,当使用其他的应用发送广播,就必须加次权限)
   
   
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.launcher" >
<permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
android:description="@string/permdesc_install_shortcut"
android:label="@string/permlab_install_shortcut"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="dangerous" />
<permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"
android:description="@string/permdesc_uninstall_shortcut"
android:label="@string/permlab_uninstall_shortcut"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="dangerous" />
<application>
....省略
<!-- Intent received used to install shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT" >
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>
<!-- Intent received used to uninstall shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.UninstallShortcutReceiver"
android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT" >
<intent-filter>
<action android:name="com.android.launcher.action.UNINSTALL_SHORTCUT" />
</intent-filter>
</receiver>
</application>
</manifest>
在其他应用中,需要在桌面创建该应用的快捷方式的,就需要发送一个广播到launcher的receiver,如果不设置user-permission权限,那么系统就获取不到你发送的信息,所以看如下代码
权限的添加:
   
   
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
下面是发送广播为应用添加快捷方式的代码:
   
   
@Override
public void onClick(View v) {
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra("duplicate", false);
String appName = getActivity().getApplicationInfo().loadLabel(
getActivity().getPackageManager()).toString();
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getActivity(),
R.drawable.ic_launcher));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(getActivity(),
MainActivity.class);
intent.setComponent(cn);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
getActivity().sendBroadcast(shortcutIntent);
}
注意:如果你在应用用没有添加权限,那么应用不会报错,也没有提示信息,但是快捷方式不会创建成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值