Evernote SDK for Android
Evernote API version 1.23
This SDK wraps the Evernote Cloud API and provides OAuth authentication functionality. The SDK is provided as an Android Library project that can be included in your application.
In order to use the code in this SDK, you need to obtain an API key from http://dev.evernote.com/documentation/cloud. You'll also find full API documentation on that page.
In order to run the sample code, you need a user account on the sandbox service where you will do your development. Sign up for an account at https://sandbox.evernote.com/Registration.action
The instructions below assume you have the latest Android SDK and API 17 installed. The instructions for eclipse are based on Eclipse Juno and latest Android development tools. The instructions for Intellij are based on Intellij IDEA 12 Community Edition and is our recommended IDE.
The sample application HelloEDAM demonstrates how to use the Evernote SDK for Android to authentication to the Evernote service using OAuth, then access the user's Evernote account. The sample code provides mutliple activities that show notebook listing, note creation, and resource creation in two scenarios: A plain text note creator and an image saver.
To build and run the sample project from Eclipse:
src
directory and open com.evernote.android.sample.ParentActivity.java
ParentActivity.java
, fill in your Evernote API consumer key and secret.To build and run the sample project from Intellij:
src
directory and open com.evernote.android.sample.ParentActivity.java
ParentActivity.java
, fill in your Evernote API consumer key and secret.There are two ways to include the SDK in your project: by including and building the Android Library Project in your IDE or by using Maven.
If you build your app using Maven, you can simply add the Evernote SDK for Android as a dependency in your pom.xml.
Add the Evernote SDK for Android as a dependency:
<dependency>
<groupId>com.evernote</groupId>
<artifactId>android-sdk</artifactId>
<version>1.1</version>
<type>apklib</type>
</dependency>
AndroidManifest.xml
The SDK's OAuth functionality is implemented as an Android Activity that must be declared in your app's AndroidManifest.xml
. Simply copy and paste the following snippet into your AndroidManifest.xml
within the application section:
<activity android:name="com.evernote.client.android.EvernoteOAuthActivity" android:configChanges="orientation|keyboardHidden" />
EvernoteSession
Define your app credentials (key, secret, and host). See http://dev.evernote.com/documentation/cloud/
private static final String CONSUMER_KEY = "Your consumer key";
private static final String CONSUMER_SECRET = "Your consumer secret";
private static final EvernoteSession.EvernoteService EVERNOTE_SERVICE = EvernoteSession.EvernoteService.SANDBOX;
When your app starts, initialize the EvernoteSession singleton that has all of the information that is needed to authenticate to Evernote. The EvernoteSession instance of saved statically and does not need to be passed between activities. The better option is to run getInstance(...) in your onCreate() of the Application object or your parent Activity object.
mEvernoteSession = EvernoteSession.getInstance(this, CONSUMER_KEY, CONSUMER_SECRET, EVERNOTE_SERVICE);
In our sample app, we have a "Sign in to Evernote" button that initiates the authentication process. You might choose to do something similar, or you might simply initiate authentication the first time that the user tries to access Evernote-related functionality.
mEvernoteSession.authenticate(this);
The Activity that completes the OAuth authentication automatically determines if the User is on the Evernote service or the Yinxiang service and configures the end points automatically.
onActivityResult
You can check whether authentication was successful by watching for the Evernote OAuth Activity in onActivityResult
. If authentication is successful, you can start using the Evernote API.
@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;
}
}
ClientFactory
to create Async ClientsCalling EvernoteSession.getClientFactory()
will give you access to createNoteStore()
, createUserStore()
, and createBusinessNoteStore()
. These objects return Asynchronously wrapped Client
objects that allow you to interact with the Evernote API over Thrift using a callback interface OnClickCallback<T>
.
If the underlying NoteStore.Client
or UserStore.Client
object is needed, access via the getClient()
method. Browse the API JavaDocs at http://dev.evernote.com/documentation/reference/javadoc/
AsyncNoteStore
to access primary methods for personal note datamEvernoteSession.getClientFactory().createNoteStore();
AsyncUserStore
to access User related methodsmEvernoteSession.getClientFactory().createUserStore();
AsyncBusinessNoteStoreClient
to access Business NotebooksmEvernoteSession.getClientFactory().createBusinessNoteStore();
AsyncLinkedNoteStoreClient
to access shared notebooksmEvernoteSession.getClientFactory().createLinkedNoteStore(linkedNotebook);
AsyncNoteStoreClient
to make asynchronous API callspublic void listNotebooks() throws TTransportException {
if (mEvernoteSession.isLoggedIn()) {
mEvernoteSession.getClientFactory().createNoteStoreClient().listNotebooks(new OnClientCallback<List<Notebook>>() {
@Override
public void onSuccess(final List<Notebook> notebooks) {
List<String> namesList = new ArrayList<String>(notebooks.size());
for (Notebook notebook : notebooks) {
namesList.add(notebook.getName());
}
String notebookNames = TextUtils.join(", ", namesList);
Toast.makeText(getApplicationContext(), notebookNames + " notebooks have been retrieved", Toast.LENGTH_LONG).show();
}
@Override
public void onException(Exception exception) {
Log.e(LOGTAG, "Error retrieving notebooks", exception);
}
});
}
}
public void createNote(String title, String content) throws TTransportException {
if (mEvernoteSession.isLoggedIn()) {
Note note = new Note();
note.setTitle(title);
note.setContent(EvernoteUtil.NOTE_PREFIX + content + EvernoteUtil.NOTE_SUFFIX);
mEvernoteSession.getClientFactory().createNoteStoreClient().createNote(note, new OnClientCallback<Note>() {
@Override
public void onSuccess(final Note data) {
Toast.makeText(getApplicationContext(), data.getTitle() + " has been created", Toast.LENGTH_LONG).show();
}
@Override
public void onException(Exception exception) {
Log.e(LOGTAG, "Error creating note", exception);
}
});
}
}
AsyncBusinessNoteStoreClient
to Access Evernote Business dataAsyncBusinessNoteStore
This notestore is not long lived, the Business authentication token expires frequently and is refreshed if needed in the createBusinessNoteStore()
method
Example using the synchronous business methods inside a background thread to create a note in a business account
new Thread(new Runnable() {
@Override
public void run() {
try {
// Is the User member of a business
if(mEvernoteSession.getClientFactory().createUserStoreClient().isBusinessUser()) {
//Create an AsyncBusinessNoteStoreClient, gets valid auth token
AsyncBusinessNoteStoreClient client = mEvernoteSession.getClientFactory().createBusinessNoteStoreClient();
List<LinkedNotebook> notebooks = client.listNotebooks();
//If the user has any business notebooks
if(notebooks.size() > 0) {
//Create a note in the first one
Note note = new Note();
note.setTitle("New Note");
note.setContent(EvernoteUtil.NOTE_PREFIX + "Content of Note" + EvernoteUtil.NOTE_SUFFIX);
final Note createdNote = client.createNote(note, notebooks.get(0));
//Update user on UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), createdNote.getTitle() + " has been created.", Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.d(LOGTAG, "Not a business User");
}
} catch(Exception exception) {
Log.e(LOGTAG, "Error received::", exception);
}
}
}).start();
Copyright (c) 2007-2012 by Evernote Corporation, All rights reserved.
Use of the source code and binary libraries included in this package
is permitted under the following terms:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.