问题
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.
回答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>
回答2:
i m not finding anywhere this permission: Manifest.permission.CONTACTS
there is only two contact permission:
- READ_CONTACTS
- WRITE_CONTACTS
you should use this two. only CONTACTS will give you error.
回答3:
Update last version Android Support Repository
And in Android Private Library only one and last version android-support-v4.jar added.
回答4:
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.
来源:https://stackoverflow.com/questions/39342237/android-marshmallow-permission-for-my-eclipse-project-not-working