0

I have created an application using eclipse IDE. And now its crashing on Marshmallow for various permissions say contact. After lot of searching I come up with no result.

It is showing error in checkSelfPermission, requestPermissions etc. on CONTACT from Manifest.permission.CONTACTS.

I think the solutions are working on android studio projects. So let me know same for eclipse project if any one know it. enter image description here

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
Mats
  • 43
  • 1
  • 8

4 Answers4

0

i m not finding anywhere this permission: Manifest.permission.CONTACTS

there is only two contact permission:

  1. READ_CONTACTS
  2. WRITE_CONTACTS

you should use this two. only CONTACTS will give you error.

Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67
  • Hello Sagar, Actaully i followed one sample after googling and that code was from there only. I have implemented if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.CONTACTS) != PackageManager.PERMISSION_GRANTED) but getting red line error : The method checkSelfPermission(HomeActivity, String) is undefined for the type ContextCompat – Mats Sep 06 '16 at 08:01
0

Update last version Android Support Repository enter image description here

And in Android Private Library only one and last version android-support-v4.jar added.

enter image description here

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

Appart from the READ_CONTACTS/WRITE_CONTACTS issue: in order to solve the checkSelfPermission() not found in Eclipse you can add android-support-compat.jar as taken from https://github.com/dandar3/android-support-compat/tree/28.0.0 to the libs folder of your Eclipse project and compile again.

mougino
  • 33
  • 4
-1

Full working demo

You are making mistake of here there is no permission like CONTACTS only there is READ_CONTACTS and WRITE_CONTACTS

correct is Manifest.permission.READ_CONTACTS; instead of Manifest.permission.CONTACTS;

public class MainActivity extends AppCompatActivity {

    private Context context;
    private Button button;
    private static final int REQUEST_RUNTIME_PERMISSION = 123;
    private String permission = Manifest.permission.READ_CONTACTS;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (CheckPermission(MainActivity.this, permission)) {
                    // you have permission go ahead
                    YouCanReadContactNow();
                } else {
                    // you do not have permission go request runtime permissions
                    RequestPermission(MainActivity.this, permission, REQUEST_RUNTIME_PERMISSION);
                }
            }
        });
    }

    private void YouCanReadContactNow() {
    }


    @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

        switch (permsRequestCode) {
            case REQUEST_RUNTIME_PERMISSION: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // you have permission go ahead
                    YouCanReadContactNow();
                } else {
                    // you do not have permission show toast.
                }
                return;
            }
        }
    }

    public void RequestPermission(Activity thisActivity, String Permission, int Code) {
        if (ContextCompat.checkSelfPermission(thisActivity,
                Permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Permission)) {
            } else {
                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Permission},
                        Code);
            }
        }
    }

    public boolean CheckPermission(Context context, String Permission) {
        if (ContextCompat.checkSelfPermission(context,
                Permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }
}

layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="horizontal">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:text="Request contact permissions"
        android:textSize="20dp" />

</RelativeLayout>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • Thanks Sohil. But I am getting issues on : The method checkSelfPermission(Activity, String) is undefined for the type ContextCompat , The method shouldShowRequestPermissionRationale(Activity, String) is undefined for the type ActivityCompat , The method requestPermissions(Activity, String[], int) is undefined for the type ActivityCompat , The method checkSelfPermission(Context, String) is undefined for the type ContextCompat – Mats Sep 06 '16 at 10:07
  • have you add the appcompat v4 lib? – Sohail Zahid Sep 06 '16 at 11:04
  • why didt you move to the android studio? – Sohail Zahid Sep 06 '16 at 11:04
  • No Sohail, It is having appcompat_v7 library. And the project is build with eclipse so trying there. And dont have knowledge about Android studio. Will install and study – Mats Sep 06 '16 at 11:15
  • How can the answer is acceptable if it is only for android studio. question was asked for eclipse only – Kamleshwer Purohit Nov 22 '18 at 11:26