标签:sms weight als app fragment comm activity line wrapper
allprojects {
repositories {
maven { url ‘https://jitpack.io‘ }
}
}
allprojects {
repositories {
maven { url ‘https://jitpack.io‘ }
}
}
implementation ‘com.github.tbruyelle:rxpermissions:0.10.2‘
implementation ‘com.github.tbruyelle:rxpermissions:0.10.2‘
final RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity or Fragment instance
final RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity or Fragment instance
// Must be done during an initialization phase like onCreate
rxPermissions.request(Manifest.permission.CAMERA)
.subscribe(granted -> {
if (granted) { // Always true pre-M
// I can control the camera now
} else {
// Oups permission denied
}
});
// Must be done during an initialization phase like onCreate
rxPermissions.request(Manifest.permission.CAMERA)
.subscribe(granted -> {
if (granted) { // Always true pre-M
// I can control the camera now
} else {
// Oups permission denied
}
});
RxView.clicks(view)
.compose(new RxPermissions(this).ensure(Manifest.permission.CAMERA))
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
RxView.clicks(view)
.compose(new RxPermissions(this).ensure(Manifest.permission.CAMERA))
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
RxPermissions(FragmentActivity activity) //不可以是普通 Activity ,因为他的原理是通过 FragmentManager 操作 Fragment 实现的
RxPermissions(Fragment fragment)
RxPermissions(FragmentActivity activity) //不可以是普通 Activity ,因为他的原理是通过 FragmentManager 操作 Fragment 实现的
RxPermissions(Fragment fragment)
Observable<Boolean> request(String... permissions)
<T> ObservableTransformer<T, Boolean> ensure(final String... permissions) //与 compose 操作符一起使用
Observable<Boolean> request(String... permissions)
<T> ObservableTransformer<T, Boolean> ensure(final String... permissions) //与 compose 操作符一起使用
Observable<Permission> requestEach(String... permissions) //申请了多少个权限就回调多少次,相当于 flatMap 的效果
Observable<Permission> requestEachCombined(String... permissions) //只回调一次,回调对象的的属性值是多个原始对象的组合值
<T> ObservableTransformer<T, Permission> ensureEach(final String... permissions)
<T> ObservableTransformer<T, Permission> ensureEachCombined(final String... permissions)
Observable<Permission> requestEach(String... permissions) //申请了多少个权限就回调多少次,相当于 flatMap 的效果
Observable<Permission> requestEachCombined(String... permissions) //只回调一次,回调对象的的属性值是多个原始对象的组合值
<T> ObservableTransformer<T, Permission> ensureEach(final String... permissions)
<T> ObservableTransformer<T, Permission> ensureEachCombined(final String... permissions)
boolean isGranted(String permission) //是否已赋予权限
void setLogging(boolean logging)
Observable<Boolean> shouldShowRequestPermissionRationale(Activity activity, String... permissions)
boolean isGranted(String permission) //是否已赋予权限
void setLogging(boolean logging)
Observable<Boolean> shouldShowRequestPermissionRationale(Activity activity, String... permissions)
public class RxPermissionsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = {"最简单实用的案例,request",
"和 RxView 一起使用,compose + ensure",
"一次请求多个权限",
"观察详细的结果,requestEach、ensureEach",
"观察详细的结果,requestEachCombined 和 ensureEachCombined",
"shouldShowRequestPermissionRationale 和 isGranted 的使用 ",};
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Arrays.asList(array)));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, RxPermissionsFragmentActivity.class);
intent.putExtra("type", position);
startActivity(intent);
}
}
public class RxPermissionsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = {"最简单实用的案例,request",
"和 RxView 一起使用,compose + ensure",
"一次请求多个权限",
"观察详细的结果,requestEach、ensureEach",
"观察详细的结果,requestEachCombined 和 ensureEachCombined",
"shouldShowRequestPermissionRationale 和 isGranted 的使用 ",};
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Arrays.asList(array)));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, RxPermissionsFragmentActivity.class);
intent.putExtra("type", position);
startActivity(intent);
}
}
public class RxPermissionsFragmentActivity extends FragmentActivity {
private TextView view;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new TextView(this);
view.setBackgroundColor(0xff00ff00);
setContentView(view);
int type = getIntent().getIntExtra("type", 0);
init(type);
}
private void init(int type) {
switch (type) {
case 0:
new RxPermissions(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(granted -> {
if (granted) { //在 Android M(6.0) 之前始终为true
Toast.makeText(this, "已赋予权限", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "已拒绝权限", Toast.LENGTH_SHORT).show();
}
});
break;
case 1:
RxView.clicks(view)
.compose(new RxPermissions(this).ensure(Manifest.permission.CAMERA))
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
break;
case 2:
new RxPermissions(this)
.request(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS)//只有所有权限都赋予了才会回调成功
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
RxView.clicks(view)
.compose(new RxPermissions(this).ensure(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS))
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
break;
case 3:
RxView.clicks(view)
.compose(new RxPermissions(this).ensureEach(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS))
.subscribe(permission -> {//申请了多少个权限就回调多少次,相当于 flatMap 的效果
if (permission.granted) {
Toast.makeText(this, "已赋予权限:" + permission.name, Toast.LENGTH_SHORT).show();
} else if (permission.shouldShowRequestPermissionRationale) { //拒绝了权限,但是没有勾选"ask never again"
Toast.makeText(this, "已拒绝权限:" + permission.name + " ,但可以继续申请", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "已拒绝权限:" + permission.name + " ,并且不会再显示", Toast.LENGTH_SHORT).show();
}
});
break;
case 4:
RxView.clicks(view)
.compose(new RxPermissions(this).ensureEachCombined(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS))
.subscribe(permission -> {//只回调一次,回调的 permission 的属性值是多个原始 permission 的组合值
if (permission.granted) {
Toast.makeText(this, "已赋予全部所需的权限:" + permission.name, Toast.LENGTH_SHORT).show();
} else if (permission.shouldShowRequestPermissionRationale) { //拒绝了权限,但是没有勾选"ask never again"
Toast.makeText(this, "有权限被拒绝:" + permission.name + " ,但可以继续申请", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "有权限被拒绝:" + permission.name + " ,并且不会再显示", Toast.LENGTH_SHORT).show();
}
});
break;
case 5:
boolean isGranted = new RxPermissions(this).isGranted(Manifest.permission.CAMERA);
Toast.makeText(this, "是否已赋予权限:" + isGranted, Toast.LENGTH_SHORT).show();
new RxPermissions(this)
.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS)
.subscribe(granted -> Toast.makeText(this, granted ? "全部权限已授权,或有权限被拒绝但可以继续申请" :
"有权限被拒绝,并且不会再显示", Toast.LENGTH_SHORT).show());
break;
default:
break;
}
}
}
x
public class RxPermissionsFragmentActivity extends FragmentActivity {
private TextView view;
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new TextView(this);
view.setBackgroundColor(0xff00ff00);
setContentView(view);
int type = getIntent().getIntExtra("type", 0);
init(type);
}
private void init(int type) {
switch (type) {
case 0:
new RxPermissions(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(granted -> {
if (granted) { //在 Android M(6.0) 之前始终为true
Toast.makeText(this, "已赋予权限", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "已拒绝权限", Toast.LENGTH_SHORT).show();
}
});
break;
case 1:
RxView.clicks(view)
.compose(new RxPermissions(this).ensure(Manifest.permission.CAMERA))
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
break;
case 2:
new RxPermissions(this)
.request(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS)//只有所有权限都赋予了才会回调成功
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
RxView.clicks(view)
.compose(new RxPermissions(this).ensure(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS))
.subscribe(granted -> Toast.makeText(this, granted ? "已赋予权限" : "已拒绝权限", Toast.LENGTH_SHORT).show());
break;
case 3:
RxView.clicks(view)
.compose(new RxPermissions(this).ensureEach(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS))
.subscribe(permission -> {//申请了多少个权限就回调多少次,相当于 flatMap 的效果
if (permission.granted) {
Toast.makeText(this, "已赋予权限:" + permission.name, Toast.LENGTH_SHORT).show();
} else if (permission.shouldShowRequestPermissionRationale) { //拒绝了权限,但是没有勾选"ask never again"
Toast.makeText(this, "已拒绝权限:" + permission.name + " ,但可以继续申请", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "已拒绝权限:" + permission.name + " ,并且不会再显示", Toast.LENGTH_SHORT).show();
}
});
break;
case 4:
RxView.clicks(view)
.compose(new RxPermissions(this).ensureEachCombined(Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS))
.subscribe(permission -> {//只回调一次,回调的 permission 的属性值是多个原始 permission 的组合值
if (permission.granted) {
Toast.makeText(this, "已赋予全部所需的权限:" + permission.name, Toast.LENGTH_SHORT).show();
} else if (permission.shouldShowRequestPermissionRationale) { //拒绝了权限,但是没有勾选"ask never again"
Toast.makeText(this, "有权限被拒绝:" + permission.name + " ,但可以继续申请", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "有权限被拒绝:" + permission.name + " ,并且不会再显示", Toast.LENGTH_SHORT).show();
}
});
break;
case 5:
boolean isGranted = new RxPermissions(this).isGranted(Manifest.permission.CAMERA);
Toast.makeText(this, "是否已赋予权限:" + isGranted, Toast.LENGTH_SHORT).show();
new RxPermissions(this)
.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS)
.subscribe(granted -> Toast.makeText(this, granted ? "全部权限已授权,或有权限被拒绝但可以继续申请" :
"有权限被拒绝,并且不会再显示", Toast.LENGTH_SHORT).show());
break;
default:
break;
}
}
}
标签:sms weight als app fragment comm activity line wrapper
原文地址:https://www.cnblogs.com/baiqiantao/p/9562734.html