在程序运行过程中申请权限
这里用CALL_PHONE作为实例,参考《第一行代码》第二版。
1、首先设置了一个按钮
<Button
android:id="@+id/make_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Make Call"/>
2、在AndroidManifest.xml文件中声明如下权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.runtimepermissiontest">
//只添加这一行就可以
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
...
</application>
</manifest>
如果是Android 6.0之前的版本,现在只需要在按钮中添加打电话逻辑就可以了,但在6.0之后的版本中,CALL_PHONE被列为了危险权限。所以需要申明权限。
3、编