PayPal SecurityException:需要 READ_PHONE_STATE

PayPal SecurityException: Requires READ_PHONE_STATE

我在 Paypal-sandbox 中尝试结帐时遇到一些错误,错误仅发生在 Marshmallow 版本的设备中,低于该版本的其他版本工作正常。

错误信息

Fatal Exception: java.lang.SecurityException: getDeviceId: Neither user 10179 nor current process has android.permission.READ_PHONE_STATE.
       at android.os.Parcel.readException(Parcel.java:1620)   
       at android.os.Parcel.readException(Parcel.java:1573)
       at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4207)
       at com.paypal.android.c.k.a(Unknown Source)
       at com.paypal.android.c.f.B(Unknown Source)
       at com.paypal.android.c.f.d(Unknown Source)
       at com.paypal.android.c.f.run(Unknown Source) 
       Background sticky concurrent mark sweep GC freed 25935(1829KB) AllocSpace objects, 47(6MB) LOS objects, 8% free, 87MB/95MB, paused 7.704ms total 70.036ms
       NeighborEvent{elapsedMs=2177754483, 192.168.0.1, [AC220B8E9BEB], RTM_NEWNEIGH, NUD_REACHABLE}
       START u0 {cmp=com.luulla.mobile.android/com.paypal.android.MEP.PayPalActivity (has extras)} from uid 10179 on display 0
       Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@724f030 attribute=null, token = android.os.BinderProxy@760242f

我可以知道有人遇到过这个问题吗?是Paypal-SDK引起的吗?非常感谢给定的建议和回答:)

如果没有,您需要在 运行 时获得许可。 从棉花糖开始,在清单中定义权限是不够的。 看到这个 link : https://developer.android.com/training/permissions/requesting.html

Call this class where you want to access the phone state.

public class RequestUserPermission {

private Activity activity;
// Storage Permissions
private static final int REQUEST_READ_PHONE_STATE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_PHONE_STATE,
};

public RequestUserPermission(Activity activity) {
    this.activity = activity;
}

public  void verifyStoragePermissions() {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_READ_PHONE_STATE
        );
    }
}
}

如果您的 运行 应用是 android 版本的 Marshmallow,它需要用户的许可,有两种方法可以解决这个问题。 要么在清单文件中将 android:targetSdkVersion="22" 更改为低于 23(Marshmallow)。

或者写一个函数获取权限:

// ID to identify phone permissions request 
private static final int REQUEST_READ_PHONE_PERMISSIONS = 1; 

// called this when "Checkout" button clicked
// function to get permission from user  
public void getUserPermission()
{
     // check whether "READ_PHONE_STATE" permission is granted or not
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
     {
         // we do not have the permissions, we need to request permission from user
         ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_PERMISSIONS);
         return;
     }
}

当您的应用程序请求权限时,系统会向用户显示一个对话框,您可能需要处理权限请求响应。

// Write a callback method
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    switch (requestCode)
    {
        case REQUEST_READ_PHONE_PERMISSIONS:
            // if request is cancelled, the result arrays are empty
            if (grantResults.length == 0 || grantResults[0] == PackageManager.PERMISSION_DENIED)
            {
                 // permissions denied. Show message to user with explanations
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("This app is designed for android version Marshmallow and above. Denying permission will cause unable to Checkout.");
                alert.setPositiveButton("Ok", null);
                alert.show();
            }

            // if we get permissions from user, proceed to checkout in Paypal
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                // continue with proceed to checkout 
            }
            return;
    }       
}

有一个新版本的 Paypal MPL 可以在不请求 READ_PHONE_STATE 许可的情况下在 Paypal 中结帐。请参考这个link.