如何在 RecyclerView 的适配器中获取运行时权限?请查看详情
How to get runtime permissions in adapter of a RecyclerView? Please see details
我正在开发一个应用程序,我需要以下运行时权限才能启动服务:
@TargetApi(Build.VERSION_CODES.M)
public void startFloatyForAboveAndroidL() {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PERMISSION_REQUEST_CODE);
} else {
floaty.startService();
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
floaty.startService();
} else {
Spanned message = Html.fromHtml("Please allow this permission, so <b>Floaties</b> could be drawn.");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
但是当我从适配器的 bindView()
启动服务时,我不知道在哪里编写这段代码。我在这里使用 FastAdapter。
这里是 HRequest.java
文件的代码:
public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {
public String imageURL;
public HRequest() {
}
public HRequest(String imageURL) {
this.imageURL = imageURL;
}
// Fast Adapter methods
@Override
public int getType() {
return R.id.recycler_view;
}
@Override
public int getLayoutRes() {
return R.layout.h_request_list_row;
}
@Override
public void bindView(ViewHolder holder) {
super.bindView(holder);
holder.imageURL.setText(imageURL);
}
// Manually create the ViewHolder class
protected static class ViewHolder extends RecyclerView.ViewHolder {
TextView imageURL;
public ViewHolder(View itemView) {
super(itemView);
imageURL = (TextView)itemView.findViewById(R.id.imageURL);
if (!imageURL.getText().toString().isEmpty()) {
if (imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
Picasso.with(itemView.getContext())
.load(imageURL.getText().toString())
.into(homelessImage);
} else {
Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
}
}
}
}
请告诉我如何在何处以及如何编写此代码以请求许可并防止应用程序在 Android M.
上崩溃
I'm developing an app in which I need the following runtime permission to start a service
从技术上讲,这不是运行时权限,但您需要该代码或类似的东西。
I have no idea where to write this code
我会在填充 RecyclerView
之前执行此操作。例如,当用户启动 activity 时,如果您没有权限,则显示设置屏幕,只有在您有权限后才在 RecyclerView
上设置适配器。
我正在开发一个应用程序,我需要以下运行时权限才能启动服务:
@TargetApi(Build.VERSION_CODES.M)
public void startFloatyForAboveAndroidL() {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PERMISSION_REQUEST_CODE);
} else {
floaty.startService();
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
floaty.startService();
} else {
Spanned message = Html.fromHtml("Please allow this permission, so <b>Floaties</b> could be drawn.");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
但是当我从适配器的 bindView()
启动服务时,我不知道在哪里编写这段代码。我在这里使用 FastAdapter。
这里是 HRequest.java
文件的代码:
public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {
public String imageURL;
public HRequest() {
}
public HRequest(String imageURL) {
this.imageURL = imageURL;
}
// Fast Adapter methods
@Override
public int getType() {
return R.id.recycler_view;
}
@Override
public int getLayoutRes() {
return R.layout.h_request_list_row;
}
@Override
public void bindView(ViewHolder holder) {
super.bindView(holder);
holder.imageURL.setText(imageURL);
}
// Manually create the ViewHolder class
protected static class ViewHolder extends RecyclerView.ViewHolder {
TextView imageURL;
public ViewHolder(View itemView) {
super(itemView);
imageURL = (TextView)itemView.findViewById(R.id.imageURL);
if (!imageURL.getText().toString().isEmpty()) {
if (imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
Picasso.with(itemView.getContext())
.load(imageURL.getText().toString())
.into(homelessImage);
} else {
Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
}
}
}
}
请告诉我如何在何处以及如何编写此代码以请求许可并防止应用程序在 Android M.
上崩溃I'm developing an app in which I need the following runtime permission to start a service
从技术上讲,这不是运行时权限,但您需要该代码或类似的东西。
I have no idea where to write this code
我会在填充 RecyclerView
之前执行此操作。例如,当用户启动 activity 时,如果您没有权限,则显示设置屏幕,只有在您有权限后才在 RecyclerView
上设置适配器。