Wei Loon Wong
Wei Loon Wong

Reputation: 460

PayPal SecurityException: Requires READ_PHONE_STATE

I have some errors when trying to checkout in Paypal-sandbox, errors only occurs in devices with version Marshmallow, other version below than that were work fine.

Error message

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

May I know anyone hit this issues ? Is it caused by Paypal-SDK ? Many thanks for the given suggestion and answer :)

Upvotes: 2

Views: 998

Answers (4)

Wei Loon Wong
Wei Loon Wong

Reputation: 460

there is a new released of Paypal MPL that enable to checkout in Paypal without requesting READ_PHONE_STATE permission. Please refer to this link.

Upvotes: 1

User7777777
User7777777

Reputation: 36

If you're running app in android version Marshmallow, it requires permission from user, there are 2 ways in solving this. Either you change your android:targetSdkVersion="22" below than 23(Marshmallow) in manifest file.

Or write a function to get permission:

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

When your app requests permissions, system will shows a dialog box to user, you might want to handle the permissions request response.

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

Upvotes: 2

MinnuKaAnae
MinnuKaAnae

Reputation: 1646

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

Upvotes: 0

Prashant
Prashant

Reputation: 1613

You need to take permissions at run time if you didn't. Starting from marshmallow defining permissions in manifest isn't enough. see this link : https://developer.android.com/training/permissions/requesting.html

Upvotes: 1

Related Questions