我的Eclipse项目的Android棉花糖权限无法正常工作 [英] Android Marshmallow Permission for my eclipse project not working

查看:53
本文介绍了我的Eclipse项目的Android棉花糖权限无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Eclipse IDE创建了一个应用程序.联系人说,现在它因各种权限而崩溃在棉花糖上.经过大量搜索,我没有结果.

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.

在清单(Manifest.permission.CONTACTS)的CONTACT上显示checkSelfPermissionrequestPermissions等错误.

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

我认为这些解决方案适用于android studio项目.因此,如果有人知道我的日食项目,请让我知道.

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

推荐答案

完整的演示

您在这里犯了一个错误,没有像CONTACTS这样的权限,只有READ_CONTACTSWRITE_CONTACTS

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

正确的是Manifest.permission.READ_CONTACTS;而不是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;
        }
    }
}

布局

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

这篇关于我的Eclipse项目的Android棉花糖权限无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆