We recently published a new Force.com toolkit for Android. The original version of the toolkit only supported the traditional username/password authentication protocol whereby the Android app had to implement a custom UI (i.e. an Android 'Activity') to capture the user's Salesforce credentials and use them to make the 'login' call in the toolkit. The toolkit has now been updated to also support the OAuth 2.0 User-Agent flow. Though you still have the option of using username/password authentication, the OAuth protocol is especially well suited for authenticating mobile applications and it is therefore recommended that you use the new 'loginOAuth' method in the toolkit to authenticate with Salesforce. The Getting Started article for the Android toolkit has been updated to show how you can use the 'loginOAuth' method. As always feedback and comments are welcome.

In addition to the OAuth support, I thought I'd discuss another interesting use case for the Android toolkit that an audience member brought up during one of my Dreamforce sessions. The use case : develop an Android application that attaches a picture taken with the phone to a record in Salesforce. You can imagine all sorts of reason why such functionality would be useful (sales reps can attach invoice images to their Account record etc.) and so I thought I'd do some quick prototyping to show how easy it would be to do this with the Force.com toolkit for Android.

The entire code for this example is included in the sample application that is bundled with the toolkit in GitHub repository. To test it, launch the sample application in your Android emulator, invoke either the 'Login' or 'OAuth Login' action from the main menu to login to Salesforce. Once successfully logged in, invoke the 'Retrieve' action from the main menu and retrieve any record from your Salesforce Org by providing the query fields, SObject Type and record Id and invoking 'Execute'. Once the record is retrieved, invoke the 'Attach Picture' button. In the Android emulator, this will bring up a moving checkbox image (this is how the Android emulator displays the camera function). Take a picture by pressing the 'Search' icon and this will attach the 'hardcoded' sample image that the emulator provides to the respective record in Salesforce.

The salient toolkit code that posts the picture as an Attachment record is highlighted below (you can review the 'PicSample.java' class in the sample application for the entire code).

class SavePhotoTask extends AsyncTask
{
    @Override
    protected String doInBackground(byte[]... jpeg) {
        SharedPreferences savedSession =                 getApplicationContext().getSharedPreferences("SObject-Id", Context.MODE_PRIVATE);
        String sobjectId = savedSession.getString("Id", null);
        Salesforce.init(getApplicationContext());
        SObject attach = new SObject();
        attach.setType("Attachment");
        attach.setField("parentId", sobjectId);
        String image = Base64.encodeToString(jpeg[0], Base64.DEFAULT);
        attach.setField("body", image);
        attach.setField("ContentType", "image/jpg");
        attach.setField("Name", "SamplePicture");
        ArrayList objs = new ArrayList();
        objs.add(attach);
        Salesforce.create(objs, new CreateResponseListener());
        return(null);
    }
}

As can be seen above, the code required to add the picture as an Attachment is trivial. A new SObject is instantiated, the relevant fields are set and the 'create' method of the toolkit is invoked to create a new Attachment record. 

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS