Search code examples
androidandroid-recyclerviewandroid-6.0-marshmallowandroid-permissionsfastadapter

How to get runtime permissions in adapter of a RecyclerView? Please see details


I'm developing an app in which I need the following runtime permission to start a service:

@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();
            }
        }

But as I'm starting the service from bindView() of the adapter, I have no idea where to write this code. I 'm using FastAdapter here.

Here's HRequest.java file's code:

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();
            }

            }
        }

    }

Please let me know how where and how can I write this code to ask for permission and preventing the app from crashing on Android M.


Solution

  • I'm developing an app in which I need the following runtime permission to start a service

    Technically, that's not a runtime permission, though you need that code or something like it.

    I have no idea where to write this code

    I would do it before populating the RecyclerView. For example, when the user starts the activity, if you do not have permission, show the Settings screen, and only set the adapter on the RecyclerView once you have permission.