如何在eclipse中为棉花糖授予从收件箱中读取短信运行时权限

pnwntuvh  于 2022-09-21  发布在  Eclipse
关注(0)|答案(3)|浏览(226)

我已应用了多个代码,但它显示了check SelfPermission()和ShordShowRequestPermissionRationale方法不退出,请帮助我。

我使用了Brodcast Receiver类来阅读短信,它对果冻到棒棒糖很好,但不适合棉花糖和比果冻更低的版本,比如冰淇淋和桑维奇,我怎么才能让应用程序兼容所有的Android版本,

请一步一步地指导我,因为我是新手,请给我举个例子。

Hear是我的阅读短信权限代码..

public void getpermission(){

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(MainActivity,
                    Manifest.permission.READ_SMS)
            != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.READ_SMS},
                    REQUEST_CODE_ASK_SINGLE_PERMISSION);

            // REQUEST_CODE_ASK_SINGLE_PERMISSION is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
3vpjnl9f

3vpjnl9f1#

您可以通过在您的活动中扩展这个给定的类来做到这一点,

public class BasePermissionAppCompatActivity extends AppCompatActivity {

private final static String APP_NAME = "APP_NAME";
private final static int REQUEST_READ_SMS_PERMISSION = 3004;
public final static String READ_SMS_PERMISSION_NOT_GRANTED = "Please allow " + APP_NAME + " to access your SMS from setting";

RequestPermissionAction onPermissionCallBack;

private boolean checkReadSMSPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

public void getReadSMSPermission(RequestPermissionAction onPermissionCallBack) {
    this.onPermissionCallBack = onPermissionCallBack;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!checkReadSMSPermission()) {
            requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_READ_SMS_PERMISSION);
            return;
        }
    }
    if (onPermissionCallBack != null)
        onPermissionCallBack.permissionGranted();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        if (REQUEST_READ_SMS_PERMISSION == requestCode) {
            // TODO Request Granted for READ_SMS.
            System.out.println("REQUEST_READ_SMS_PERMISSION Permission Granted");
        }
        if (onPermissionCallBack != null)
            onPermissionCallBack.permissionGranted();

    } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
        if (REQUEST_READ_SMS_PERMISSION == requestCode) {
            // TODO REQUEST_READ_SMS_PERMISSION Permission is not Granted.
            // TODO Request Not Granted.

            // This code is for get permission from setting.
            //final Intent i = new Intent();
            //i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            //i.addCategory(Intent.CATEGORY_DEFAULT);
            //i.setData(Uri.parse("package:" + getPackageName()));
            //i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            //i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            //startActivity(i);
        }
        if (onPermissionCallBack != null)
            onPermissionCallBack.permissionDenied();
    }
}

public interface RequestPermissionAction {
    void permissionDenied();

    void permissionGranted();
}

}

就像这样

public class ActivityMain extends BasePermissionAppCompatActivity

现在,在您的ActivityMain中,您可以通过以下代码块获得权限

getReadSMSPermission(new RequestPermissionAction() {
@Override
public void permissionDenied() {
    // Call Back, when permission is Denied
    // TODO, task after permission is not greante
}

@Override
public void permissionGranted() {
    // Call Back, when permission is Granted
    // TODO, task after permission is greante
}
});

还有一件事,您需要在清单中添加权限

<uses-permission android:name="android.permission.READ_SMS"/>
gtlvzcf8

gtlvzcf82#

您应该按照文档要求阅读文档https://developer.android.com/training/permissions/requesting.html

private static finat int MY_PERMISSIONS_REQUEST_READ_SMS = 101;

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_SMS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_SMS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_SMS},
                MY_PERMISSIONS_REQUEST_READ_SMS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

并且您必须处理权限请求响应。

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_SMS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
kse8i1jr

kse8i1jr3#

使用以下方法:

public boolean checkPermissionSMS(final Context context, final String[] PERMISSIONS, final int Requestcode) {
            int currentApiversion = Build.VERSION.SDK_INT;

            if (currentApiversion >= Build.VERSION_CODES.M) {
                if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_SMS)) {

                        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                        alertBuilder.setCancelable(true);
                        alertBuilder.setTitle("Permission necessary");
                        alertBuilder.setMessage("Read SMS permission is necessary");
                        alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                               //in fragment
                        requestPermissions(PERMISSIONS,Requestcode);

//in activity
                  ActivityCompact.requestPermissions(YourActivity.this,PERMISSIONS,Requestcode);
                            }
                        });
                        AlertDialog alert = alertBuilder.create();
                        alert.show();

                    } else {
//in fragment
                        requestPermissions(PERMISSIONS,Requestcode);

//in activity
                  ActivityCompact.requestPermissions(YourActivty.this,PERMISSIONS,Requestcode);
                    }
                    return false;

                } else
                    return true;
            } else
                return true;

        }

and then in onrequestPermissionresult:

     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            if(requestCode==100)
            {
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
                {
                    //do your functionality
                }
            }
        }

并按如下方式调用该方法:

checkPermissionSMS(context,new String[]{Manifest.permission.READ_SMS},100);

当您以这样的方式请求权限时:

requestPermissions(PERMISSIONS,Requestcode);

然后在调用片段中搜索onRequestPermissionResult方法

当您以这样的方式请求权限时:

ActivityCompact.requestPermissions(YourActivity.this,PERMISSIONS,Requestcode);

然后在调用活动中搜索onRequestPermissionREsult方法。

相关问题