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.
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>