Skip to content

Commit

Permalink
doc(demo):更新文档和demo
Browse files Browse the repository at this point in the history
  • Loading branch information
junhua committed Jun 12, 2019
1 parent 0d4b6f0 commit fd0c189
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

- 链式编程
- 运行时权限申请
- 支持多个权限同时申请
- 运行时权限组申请
- 运行时权限和权限组混合申请
- 支持多个权限并行申请
- 支持多个权限串行申请
- 支持特殊权限申请,如REQUEST_INSTALL_PACKAGES,SYSTEM_ALERT_WINDOW,ACCESS_NOTIFICATION_POLICY,WRITE_SETTINGS
- 最小支持android 14
- 多ROM多版本适配
Expand Down Expand Up @@ -60,7 +63,9 @@ PermissionAgent.getInstance()
})
.apply();
```
#### 一次请求多个权限
#### 并行请求多个权限

并行请求时,结果会同时返回。当用户取消单个权限时,其他权限也不会请求并回调拒绝。
```java
PermissionAgent.getInstance()
.request(Manifest.permission.CAMERA, Manifest.permission.WRITE_CONTACTS)
Expand Down Expand Up @@ -89,6 +94,38 @@ PermissionAgent.getInstance()
.apply();
```

#### 串行请求多个权限

串行请求时,权限会顺序请求,当前一个请求处理完成后才会请求后一个权限。
注意:``AgentExecutor``必须回调``execute()``或者``cancel()``之一,才能执行后续请求。
```java
PermissionAgent.getInstance()
.requestEach(Manifest.permission_group.CONTACTS, Manifest.permission.ACCESS_COARSE_LOCATION)
.onGranted(new OnGrantedCallback<List<String>>() {
@Override
public void onGranted(List<String> permissions) {
toast("onGranted() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onGranted() called with: permissions = [" + permissions + "]");
}
})
.onDenied(new OnDeniedCallback<List<String>>() {
@Override
public void onDenied(List<String> permissions) {
toast("onDenied() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onDenied() called with: permissions = [" + permissions + "]");
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(List<String> permissions, AgentExecutor executor) {
executor.execute();
toast("onRationale() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onRationale() called with: permissions = [" + permissions + "], executor = [" + executor + "]");
}
})
.apply();
```

#### 特殊权限

使用``SpecialPermission``枚举选择需要申请的特殊权限,其他操作不变
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
// implementation project(':agent')
implementation 'cn.junhua.android:permission-agent:1.0.1'
implementation project(':agent')
// implementation 'cn.junhua.android:permission-agent:1.0.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

Expand Down Expand Up @@ -42,6 +43,12 @@ public void onClick(View v) {
requestLocation();
}
});
findViewById(R.id.tv_serial_requests).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestSerialRequests();
}
});
findViewById(R.id.tv_amws).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -155,6 +162,34 @@ public void onRationale(List<String> permissions, AgentExecutor executor) {
.apply();
}

private void requestSerialRequests() {
PermissionAgent.getInstance()
.requestEach(Manifest.permission_group.CONTACTS, Manifest.permission.ACCESS_COARSE_LOCATION)
.onGranted(new OnGrantedCallback<List<String>>() {
@Override
public void onGranted(List<String> permissions) {
toast("onGranted() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onGranted() called with: permissions = [" + permissions + "]");
}
})
.onDenied(new OnDeniedCallback<List<String>>() {
@Override
public void onDenied(List<String> permissions) {
toast("onDenied() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onDenied() called with: permissions = [" + permissions + "]");
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(List<String> permissions, AgentExecutor executor) {
executor.execute();
toast("onRationale() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onRationale() called with: permissions = [" + permissions + "], executor = [" + executor + "]");
}
})
.apply();
}

public void requestOverlay(View view) {
PermissionAgent.getInstance()
.request(SpecialPermission.SYSTEM_ALERT_WINDOW)
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:text="并行请求危险权限" />

<TextView
android:id="@+id/tv_check_permission"
android:layout_width="match_parent"
Expand Down Expand Up @@ -40,6 +48,30 @@
android:padding="20dp"
android:text="LOCATION" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:text="串行请求危险权限" />

<TextView
android:id="@+id/tv_serial_requests"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="20dp"
android:text="group.CONTACTS + ACCESS_COARSE_LOCATION" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:text="请求特殊权限" />

<TextView
android:id="@+id/tv_amws"
android:layout_width="match_parent"
Expand All @@ -50,6 +82,7 @@
android:padding="20dp"
android:text="ACTION_MANAGE_OVERLAY_PERMISSION" />


<TextView
android:id="@+id/tv_ws"
android:layout_width="match_parent"
Expand Down

0 comments on commit fd0c189

Please sign in to comment.