PayPal SecurityException:需要READ_PHONE_STATE

时间:2016-08-30 09:03:50

标签: android android-permissions paypal

我尝试在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$3.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引起的吗?非常感谢给出的建议和答案:)

4 个答案:

答案 0 :(得分:2)

如果您在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;
    }       
}

答案 1 :(得分:1)

如果您没有,则需要在运行时获取权限。 从清单中定义权限的marshmallow开始还不够。 请看这个链接:https://developer.android.com/training/permissions/requesting.html

答案 2 :(得分:1)

有一个新发布的Paypal MPL,可以在Paypal中结账,而无需请求READ_PHONE_STATE权限。请参阅此link

答案 3 :(得分:0)

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
        );
    }
}
}