Android中自定义权限

简介: android中为了安全性起见,对于应用预定义了很多权限,比如联系人,短信等等。而且其还支持自定义权限,下面通过代码示例,表述一下自定义权限的定义以及使用。 首先定义一个Activity: package com.
android中为了安全性起见,对于应用预定义了很多权限,比如联系人,短信等等。而且其还支持自定义权限,下面通过代码示例,表述一下自定义权限的定义以及使用。
首先定义一个Activity:
package com.cust.perm;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PrivilActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
TextView txt=new TextView(this);
txt.setText("hello from privilActivity");
layout.addView(txt);
setContentView(layout);
}
}
而后在menifest.xml文件中进行权限的声明:
<activity android:name=".PrivilActivity"
  android:permission="com.permission.my"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />

<permission android:protectionLevel="normal" android:name="com.permission.my"></permission>
请注意红色部分,核心配置。
关于<permission>标签中有很多属性,上述配置的这两个属性是必须的,其他属性含义如下:
img_23d4113423a52e06b446c357c20e759f.jpg


img_ffe86cba2d071fd15ea132d741bcf3f7.jpg


下面编写另外一个项目,该项目中对于PrivilActivity进行使用,假设该项目中有一个ClientActivity
,其功用是通过intent启动PrivilActivity
public class ClientActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClassName("com.cust.perm","com.cust.perm.PrivilActivity");
startActivity(intent);
}
});
}
}

在其manifest.xml配置如下:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ClientActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="4" />

<uses-permission android:name="com.permission.my"></uses-permission>
这样就获取了自定义权限。
目录
相关文章
|
21天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
50 1
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
40 1
|
7月前
|
API Android开发
Android 自定义最大宽度,高度, 宽高比例 Layout
Android 自定义最大宽度,高度, 宽高比例 Layout
|
4月前
|
XML 搜索推荐 Java
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
33 0
|
4月前
|
XML 前端开发 Java
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
35 1
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
25天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
7月前
|
XML Android开发 数据格式
自定义Android titleBar
自定义Android titleBar
27 0
|
8月前
|
Android开发
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
70 0