版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android6.0中怎么申请动态权限
Android6.0中怎么申请动态权限?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。如果设备运行的是Android6.0(API级别23)或更高版本,并且应用的targetSdkVersion是23或更高版本,则应用在运行时向用户请求权限。以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整组权限都可以使用了group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
group:android.permission-group.PHONE
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL
group:android.permission-group.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
group:android.permission-group.CAMERA
permission:android.permission.CAMERA
group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS
group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO
group:android.permission-group.SMS
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS以下是普通权限,只需要在AndroidManifest.xml中申请即可
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT示例代码/**
*
BaseActivity
*
*
@author
Edwin.Wu
*
@version
2017/3/17$
14:12$
*
@since
JDK1.8
*/
public
class
BaseActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate(@Nullable
Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
}
/**
*
为子类提供一个权限检查方法
*
*
@param
permissions
*
@return
*/
public
boolean
hasPermission(String...
permissions)
{
for
(String
permission
:
permissions)
{
if
(ActivityCompat.checkSelfPermission(this,
permission)
!=
PackageManager.PERMISSION_GRANTED)
{
return
false;
}
}
return
true;
}
/**
*
权限请求申请
*
*
@param
requestCode
请求码
*
@param
permissions
权限
*/
public
void
requestPermission(int
requestCode,
String...
permissions)
{
ActivityCompat.requestPermissions(this,
permissions,
requestCode);
}
@Override
public
void
onRequestPermissionsResult(int
requestCode,
@NonNull
String[]
permissions,
@NonNull
int[]
grantResults)
{
super.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
switch
(requestCode)
{
case
PermissionsConstans.WRITE_STORAGE_CODE:
if
(grantResults[0]
==
PackageManager.PERMISSION_GRANTED)
{
doSDCardPermission();
}
else
{
//TODO
提示用户权限未授予
Toast.makeText(BaseActivity.this,
"WRITE_EXTERNAL_STORAGE
权限未开启",
Toast.LENGTH_SHORT).show();
}
break;
case
PermissionsConstans.CALL_PHONE_CODE:
if
(grantResults[0]
==
PackageManager.PERMISSION_GRANTED)
{
doCallPhone();
}
else
{
//TODO
提示用户权限未授予
Toast.makeText(BaseActivity.this,
"ACTION_CALL
权限未开启",
Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
/**
*
默认的写SD权限处理
*/
protected
void
doSDCardPermission()
{
//TODO
}
/**
*
默认的打电话处理
*/
protected
void
doCallPhone()
{
//TODO
}
}/**
*
BaseActivity
*
*
@author
Edwin.Wu
*
@version
2017/3/17
11:59
*
@since
JDK1.8
*/
public
class
PermissionActivity
extends
BaseActivity
{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);
}
public
void
callPhone(View
view)
{
if
(hasPermission(android.Manifest.permission.CALL_PHONE))
{
doCallPhone();
}
else
{
requestPermission(PermissionsConstans.CALL_PHONE_CODE,
android.Manifest.permission.CALL_PHONE);
}
}
public
void
sdCardPermission(View
view)
{
if
(hasPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
doSDCardPermission();
}
else
{
requestPermission(PermissionsConstans.WRITE_STORAGE_CODE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
@Override
protected
void
doCallPhone()
{
Intent
intent
=
new
Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"
+
"10000"));
startActivity(intent);
}
@Override
protected
void
doSDCardPermission()
{
OkHttpClient
okHttpClient
=
new
OkHttpClient();
Request
request
=
new
Request
.Builder()
.url("/upload/information/20200623/125/132154.jpg")
.get()
.build();
Call
call
=
okHttpClient.newCall(request);
call.enqueue(new
Callback()
{
@Override
public
void
onFailure(Call
call,
IOException
e)
{
}
@Override
public
void
onResponse(Call
call,
Response
response)
throws
IOException
{
ResponseBody
body
=
response.body();
InputStream
is
=
body.byteStream();
int
len
=
0;
byte[]
bytes
=
new
byte[2048];
String
path
=
Environment.getExternalStorageDirectory().getAbsolutePath()
+
"/"
+
System.currentTimeMillis()
+
".jpg";
File
file
=
new
File(path);
FileOutputStream
fos
=
new
FileOutputStream(file);
while
((len
=
is.read(bytes))
!=
-1)
{
fos.write(bytes,
0,
len);
fos.flush();
}
Toast.makeText(PermissionActivity.this,
"SDCard写入成功",
Toast.LENGTH_SHORT).show();
Log.e("Edwin",
"SDCard写入成功");
}
});
}
}/**
*
PermissionGen框架
*
*
@author
Edwin.Wu
*
@version
2017/3/17
23:19
*
@since
JDK1.8
*/
public
class
PermissionGenActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission_gen);
}
public
void
openCamera(View
view)
{
PermissionGen.needPermission(this,
200,
Manifest.permission.CAMERA);
}
public
void
openGroup(View
view)
{
PermissionGen
.with(this)
.addRequestCode(100)
.permissions(
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_CONTACTS)
.request();
}
@PermissionSuccess(requestCode
=
100)
public
void
openGroupSuccess()
{
Toast.makeText(this,
"Group
Permission
Success",
Toast.LENGTH_SHORT).show();
startActivity(new
Intent(this,
ContactActivity.class));
}
@PermissionFail(requestCode
=
100)
private
void
openGroupFail()
{
Toast.makeText(this,
"Group
permission
is
not
granted",
Toast.LENGTH_SHORT).show();
}
@PermissionSuccess(requestCode
=
200)
public
void
openCameraSuccess()
{
Toast.makeText(this,
"Camera
permission
Success",
Toast.LENGTH_SHORT).show();
}
@PermissionFail(requestCode
=
200)
public
void
openCameraFail()
{
Toast.makeText(this,
"Camera
permission
is
not
granted",
Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRequestPermissionsResult(int
requestCode,
String[]
permissions,
int[]
grantResults)
{
PermissionGen.onRequestPermissionsResult(this,
requestCode,
permissions,
grantResults);
}
}/**
*
RxPermissionGen框架
*
*
@author
Edwin.Wu
*
@version
2017/3/19
14:17
*
@since
JDK1.8
*/
public
class
RxPermissionActivity
extends
AppCompatActivity
{
private
static
final
String
TAG
=
"RxPermissionsSample";
private
Camera
camera;
private
SurfaceView
surfaceView;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
RxPermissions
rxPermissions
=
new
RxPermissions(this);
rxPermissions.setLogging(true);
setContentView(R.layout.activity_rx_permission);
surfaceView
=
(SurfaceView)
findViewById(R.id.surfaceView);
RxView.clicks(findViewById(R.id.enableCamera))
//
Ask
for
permissions
when
button
is
clicked
.compose(rxPermissions.ensureEach(Manifest.permission.CAMERA))
.subscribe(new
Action1<Permission>()
{
@Override
public
void
call(Permission
permission)
{
Log.i(TAG,
"Permission
result
"
+
permission);
if
(permission.granted)
{
releaseCamera();
came
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学语文个人述职报告锦集8篇
- 现代水墨课程设计教案
- 企业业务集成与协同平台解决方案
- 养老院老人康复设施维修人员表彰制度
- 学校出纳工作总结
- 网络营销 第3版 教案汇 魏亚萍 1.2项目一定义、岗位 - 5-4信息流推广
- 房地产总企业行政规章制度
- 建筑垃圾运输合同
- 培训场地租赁协议书模板
- 公寓租赁合作合同
- 第3课光的反射(教学设计)五年级科学上册
- DL∕T 677-2018 发电厂在线化学仪表检验规程
- 马克思主义与社会科学方法论课后思考题答案全
- 部编《道德与法治》四年级上册复习教案
- 幼儿园教师职称五套试题及答案
- 幼儿园中班语言课件:《小花猫交朋友》
- 七年级历史下册教学工作计划
- 《架空输电线路直升机巡视技术导则》
- 热工基础课后答案超详细版(张学学)
- 食品工艺学(鲁东大学)智慧树知到期末考试答案2024年
- 沙门菌感染的人工智能与机器学习应用
评论
0/150
提交评论