• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

Identity Kit - Obtaining a User Address

Search This thread

XDARoni

XDA Community Manager
Obtaining a User Address
  • Function
    This API is used to display the address selection page when you app needs to use the user's address related information, such as name, contact, and detailed address. After the user selects an address on the page, it returns the address to the app.
  • Development Procedure

Step 1 Instantiate the request object by using the new UserAddressRequest method. Then, call the getUserAddress API.
Code:
UserAddressRequest req = new UserAddressRequest(); 
Task<GetUserAddressResult> task = Address.getAddressClient(this).getUserAddress(req); 
task.addOnSuccessListener(new OnSuccessListener<GetUserAddressResult>() { 
    @Override 
    public void onSuccess(GetUserAddressResult result) { 
        Log.i(TAG, "onSuccess result code:" + result.getReturnCode()); 
        try { 
            startActivityForResult(result); 
        } catch (IntentSender.SendIntentException e) { 
            e.printStackTrace(); 
        } 
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(Exception e) { 
        Log.i(TAG, "on Failed result code:" + e.getMessage()); 
    } 
});

Step 2 Display the address selection page by calling the startResolutionForResult method of Status.
Code:
private void startActivityForResult(GetUserAddressResult result) throws IntentSender.SendIntentException { 
    Status status = result.getStatus(); 
    if (result.getReturnCode() == 0 && status.hasResolution()) { 
        Log.i(TAG, "the result had resolution."); 
        status.startResolutionForResult(this, 1000); 
    } else { 
        Log.i(TAG, "the result hasn't resolution."); 
    } 
}
Step 3 After the user selects an address, call the parseIntent method of UserAddress in onActivityResult of the page and obtain the address from the returned result.
Code:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.i(TAG, "requestCode=" + requestCode + ", resultCode=" + resultCode);
    if (resultCode == Activity.RESULT_OK) {
        UserAddress userAddress = UserAddress.parseIntent(data);
        // Business process
    } else {
        // Error Handling
    }
}
 

Top Liked Posts

  • There are no posts matching your filters.
  • 6
    Obtaining a User Address
    • Function
      This API is used to display the address selection page when you app needs to use the user's address related information, such as name, contact, and detailed address. After the user selects an address on the page, it returns the address to the app.
    • Development Procedure

    Step 1 Instantiate the request object by using the new UserAddressRequest method. Then, call the getUserAddress API.
    Code:
    UserAddressRequest req = new UserAddressRequest(); 
    Task<GetUserAddressResult> task = Address.getAddressClient(this).getUserAddress(req); 
    task.addOnSuccessListener(new OnSuccessListener<GetUserAddressResult>() { 
        @Override 
        public void onSuccess(GetUserAddressResult result) { 
            Log.i(TAG, "onSuccess result code:" + result.getReturnCode()); 
            try { 
                startActivityForResult(result); 
            } catch (IntentSender.SendIntentException e) { 
                e.printStackTrace(); 
            } 
        } 
    }).addOnFailureListener(new OnFailureListener() { 
        @Override 
        public void onFailure(Exception e) { 
            Log.i(TAG, "on Failed result code:" + e.getMessage()); 
        } 
    });

    Step 2 Display the address selection page by calling the startResolutionForResult method of Status.
    Code:
    private void startActivityForResult(GetUserAddressResult result) throws IntentSender.SendIntentException { 
        Status status = result.getStatus(); 
        if (result.getReturnCode() == 0 && status.hasResolution()) { 
            Log.i(TAG, "the result had resolution."); 
            status.startResolutionForResult(this, 1000); 
        } else { 
            Log.i(TAG, "the result hasn't resolution."); 
        } 
    }
    Step 3 After the user selects an address, call the parseIntent method of UserAddress in onActivityResult of the page and obtain the address from the returned result.
    Code:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(TAG, "requestCode=" + requestCode + ", resultCode=" + resultCode);
        if (resultCode == Activity.RESULT_OK) {
            UserAddress userAddress = UserAddress.parseIntent(data);
            // Business process
        } else {
            // Error Handling
        }
    }