Android set permission inside list-view custom apdpater class

时间:2017-11-13 06:27:36

标签: android android-permissions

I am trying to set permission to make the phone call inside custom list adapter class extends from BaseAdapter from getView method.

But when touching on icon image the application crash

public View getView(int position, View convertView, ViewGroup parent) {


        final TextView  phoneTxt  = (TextView)convertView.findViewById(R.id.phone);
        ImageView phoneImage = (ImageView)convertView.findViewById(R.id.phone_icon);
        phoneImage.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
               int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
               if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                 ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, REQUEST__PHONE_CALL);
               }
               else {
                 Intent intent = new Intent(Intent.ACTION_CALL);
                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 intent.setData(Uri.parse("tel:" + phoneTxt.getText()));
                 context.startActivity(intent);
               }

        }
    });
    }


    return convertView;
}

Here is the error log

11-13 11:54:05.297: E/AndroidRuntime(9530): FATAL EXCEPTION: main
11-13 11:54:05.297: E/AndroidRuntime(9530): Process: com.myapp.myApp, PID: 9530
11-13 11:54:05.297: E/AndroidRuntime(9530): java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
11-13 11:54:05.297: E/AndroidRuntime(9530):     at com.myapp.myApp.adapter.NavDrawerListAdapter$1.onClick(NavDrawerListAdapter.java:134)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at android.view.View.performClick(View.java:5269)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at android.view.View$PerformClick.run(View.java:21556)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at android.os.Handler.handleCallback(Handler.java:815)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at android.os.Handler.dispatchMessage(Handler.java:104)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at android.os.Looper.loop(Looper.java:207)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at android.app.ActivityThread.main(ActivityThread.java:5769)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at java.lang.reflect.Method.invoke(Native Method)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-13 11:54:05.297: E/AndroidRuntime(9530):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Why this happening, is there any bug in above code?.

3 个答案:

答案 0 :(得分:2)

Looks like the context that you're using here:

ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, REQUEST__PHONE_CALL)
                                             ^^^^^^^

is ApplicationContext and not an Activity's context, wherever you're passing the context to your adapter, try passing :

YourActivity.this; // in case your list view is in an activity
getActivity(); // in case it's in a fragment

and not getApplicationContext(), which is possibly what you're doing right now.

答案 1 :(得分:1)

In your code, you are trying to cast the context to Activity but a Context is not necessarily the instance of Activity. You may have used getApplicationContext() or getApplication() as the parameter where Context is required. Consider using YourActivity.this so that it can be casted to Activity.

Or you can use my library to request permissions from any context easily.

Check the instructions at https://github.com/nabinbhandari/Android-Permissions

Eg.

Permissions.check(context, Manifest.permission.CALL_PHONE, null,
    new PermissionHandler() {
        @Override
        public void onGranted() {
            //do your task.
        }
    });

答案 2 :(得分:0)

declare global in your activity or fargment

private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 101;

make checkAndRequestPermissions method and call in your list or before list.

public static boolean checkAndRequestPermissions(Context context) {

    int callphonePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);

    List<String> listPermissionsNeeded = new ArrayList<>();

    if (callphonePermission != PackageManager.PERMISSION_GRANTED) {
       listPermissionsNeeded.add(Manifest.permission.CALL_PHONE);
    }

    if (!listPermissionsNeeded.isEmpty()) {
       ActivityCompat.requestPermissions((Activity) context, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
       return false;
    }
    return true;
}