The Evernote SDK for Android Quick Start Guide
The purpose of this guide is to show you how to install the Evernote SDK for Android, configure OAuth and briefly introduce the Evernote Cloud API.
-
1. What You Need
Before we begin, make sure you have both of the following:
- An account on
sandbox.evernote.com, Evernote's development server. Your application will be communicating with this server during development. If you don't yet have an account, you can create one here. - An Evernote API key, which consists of a Consumer Key and a Consumer Secret. If you don't have an API key, you can request one here.
- A functional development environment containing the Android SDK.
Additionally, this guide will be describing how to setup the Evernote SDK for Android to work with the Eclipse IDE. If Eclipse is not your preferred Android development tool, you'll probably need to modify how you follow the instructions in the next section so your environment is properly set up and configured.
- An account on
-
2. Getting the SDK
All of Evernote's SDKs are housed on Github. To download the Evernote SDK for Android as a zip file, visit the SDK project page on Github and click the "ZIP" button near the top of the page.
Extract the archive; the folder within the SDK that you'll be using in your Android project is the
libraryfolder.Adding the SDK to Your Project
(Note: this is boosted directly from the README file included with the Evernote SDK for Android. If, for some reason, these instructions don't get the job done, see the README file.)
- Import the Android Library Project
- Open Eclipse
- From the File menu, choose New and then Project...
- Under Android, select "Android Project from Existing Code" and click Next
- Click Browse
- Select the library directory and click Open
- Click Finish
- Add the Android Library Project as a dependency in your app
- Right-click on your project and choose "Properties"
- In the Android section, in the Library area, click Add...
- Select library from the list and click OK
- Click Java Build Path and then select the Projects tab
- Click Add...
- Select Library and click OK
- Click OK
From there, you can
importall of the various packages and classes available in the SDK. - Import the Android Library Project
-
3. Connecting to the Evernote Cloud API
All communication with the Evernote Cloud API uses the
EvernoteSessionobject. Looking at theHelloEDAM.javafile in the sample application included with the Evernote SDK for Android, we can see how to initialize and useEvernoteSession.Beginning just inside the declaration of the
HelloEDAMclass, you'll find these lines of code:HelloEDAM.java private static final String CONSUMER_KEY = "Your consumer key"; private static final String CONSUMER_SECRET = "Your consumer secret"; private static final EvernoteService EVERNOTE_HOST = EvernoteService.SANDBOX;CONSUMER_KEYandCONSUMER_SECRETare the two parts of your Evernote API key. Replace the placeholder text with your actual key and secret.EVERNOTE_HOSTis the server with which your app will be interacting. While your app is in development, this host will beEvernoteSession.HOST_SANDBOX. Once your app is ready for production use and you've had your Evernote API key activated in our production environment, you'll need to change that value toEvernoteSession.HOST_PRODUCTIONorEvernoteSession.HOST_CHINAif you're connecting to the Yinxiang Biji (Evernote China) service.At the bottom of the
onCreatemethod, you'll see a call tosetupSession()whose definition looks like this:HelloEDAM.java private void setupSession() { mEvernoteSession = EvernoteSession.getInstance(this, CONSUMER_KEY, CONSUMER_SECRET, EVERNOTE_HOST); }Under the hood,
EvernoteSessionis a singleton object; if an instance has already been created wheninitis called, that instance will be returned. Otherwise, a new instance is created and returned.The constructor takes five parameters:
- A
Contextobject.this, in this case sinceActivityis a descendent ofContextin theandroidclass hierarchy. - A consumer key (which we have defined as
CONSUMER_KEY). - A consumer secret (
CONSUMER_SECRETin the sample app). - The host we'll be connecting to (
EVERNOTE_HOSThere). - An instance of
java.io.Filewhere temporary application data will be stored. If we passnullhere (which we do, in this case), the default application temp directory—defined in the SDK—will be used.
Once
EvernoteSessionhas been initialized—asmEvernoteSessionin our app—we're almost ready to start making calls to the API. First, though, we need to authenticate. - A
-
4. Authentication
The Evernote Cloud API uses the OAuth protocol to authenticate users. Virtually all of the OAuth flow has been baked in to the Evernote SDK for Android, so authentication is actually quite simple:
HelloEDAM.java mEvernoteSession.authenticate(this);This begins the OAuth flow by prompting the user to authenticate with Evernote via a web view. Assuming they authenticate successfully, control will return to the main app view and
onActivityResultwill be called, completing the authentication process. A "stub" of this function might look like this:example.java @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { // Update UI when oauth activity returns result case EvernoteSession.REQUEST_CODE_OAUTH: if (resultCode == Activity.RESULT_OK) { // Authentication was successful, do what you need to do in your app } break; // ... default: break; } }Within the
ifblock that confirms the authentication was successful, you can add the code to make whatever changes to your application state are necessary (see theHelloEDAM.javaimplementation for a simple example).At this point,
mEvernoteSessionwill be ready to send requests to the Evernote Cloud API. -
5. Making API Calls
In the sample app, locate the
doInBackgroundmethod of theEvernoteNoteCreatorinner class. This is where virtually all of the interactions with the Evernote Cloud API take place. (We usedoInBackgroundbecause all API calls we make are network requests and should be made on their own thread).There are two classes with which you should become familiar:
NoteStoreandUserStore. These allow you to access a user's Evernote data (notes, notebooks, etc.) and account information (premium status, user name, real name, etc.), respectively. Both of these objects are created usingEvernoteSessionmethods:createNoteStoreandcreateUserStore.The body of
doInBackgroundis a fine example of the basics of the Evernote Cloud API. This method, essentially, creates a new note, attaches an image (called aResourcein Evernote API parlance) and sends the note to the API using this line of code:HelloEDAM.java createdNote = mEvernoteSession.createNoteStore().createNote(mEvernoteSession.getAuthToken(), note);mEvernoteSession.createNoteStoreis called, followed immediately by a call tocreateNote.createNotetakes two parameters: the auth token (which was obtained during the authentication process) and thenoteobject. Assuming the operation is successful,createdNotewill now contain, among other values, a GUID that uniquely identifies the note. -
6. Conclusion
The breadth of API functions available is beyond the scope of this guide, but you would do well to peruse the methods available in
NoteStoreandUserStoresince they will represent the vast majority of your app's interactions with the Evernote Cloud API.At this point, you should be familiar with how to download the Evernote SDK for Android and add it to your app, as well as configure your app to use OAuth and your Evernote Cloud API key. If you have any problems, you can search the Evernote Developer Forum or visit developer support.