Last active
December 16, 2015 23:38
-
-
Save mosil/5514859 to your computer and use it in GitHub Desktop.
Evernote SDK Sample Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//定義三個 Evernote SDK 一定要用到的三個參數 | |
private static final String CONSUMER_KEY = "改成您的 CONSUMER_KEY"; | |
private static final String CONSUMER_SECRET = "改成您的 CONSUMER_SECRET"; | |
private static final EvernoteSession.EvernoteService EVERNOTE_SERVICE = EvernoteSession.EvernoteService.SANDBOX; | |
//宣告 EvernoteSession 物件 | |
protected EvernoteSession mEvernoteSession; | |
//宣告兩個要串接介面元素物件 | |
private Button mBtnLogin; | |
private TextView mTxtToken; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//實作 EvernoteSession 物件,要放入四個參數 | |
//依序號為 Context, Key 字串, Secret 字串, Evernote 的 service 類別 | |
mEvernoteSession = | |
EvernoteSession.getInstance(this, CONSUMER_KEY, CONSUMER_SECRET, EVERNOTE_SERVICE); | |
//將兩個物件串接介面 | |
mBtnLogin = (Button)findViewById(R.id.btn_login); | |
mTxtToken = (TextView)findViewById(R.id.txt_my_token); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
//在這邊執行畫面要呈現登入或登出的狀態 | |
//若是我們進到mEvernoteSession.authenticate()這個授權函式去看的話 | |
//我們可以看到,這個函式在正確登入時是採用startActivityForResult的方式 | |
//所以,其實我們也可以使用onActivityResult來接回正確登入後要做的動作 | |
//不過因為本範例的狀況是在一開始進來以及正確登入時都要確認授權狀況 | |
//所以就集中在onResume這個狀態裡執行登入登出的判斷了 | |
setLogin(); | |
} | |
/** | |
* 設定登入或是登出要呈現的畫面 | |
* */ | |
private void setLogin(){ | |
if(mEvernoteSession.isLoggedIn()){ | |
//已登入,將按鈕文字改為「登出」 | |
mBtnLogin.setText(getString(R.string.logout)); | |
//將偵聽式設定為執行登出的偵聽式 | |
mBtnLogin.setOnClickListener(lisLogout); | |
//若是登入狀態,將此字串更改成我們從 Evernote Service 所取得的 Token | |
mTxtToken.setText(mEvernoteSession.getAuthToken()); | |
} else { | |
//未登入,將按鈕文字改為「登入」 | |
mBtnLogin.setText(getString(R.string.login)); | |
//將偵聽式設定為執行登入的偵聽式 | |
mBtnLogin.setOnClickListener(lisLogin); | |
//在尚未登入的狀態,此字串設定成 "Hello world!" | |
mTxtToken.setText(R.string.hello_world); | |
} | |
} | |
/** | |
* 執行登入的偵聽式 | |
* */ | |
private OnClickListener lisLogin = new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
mEvernoteSession.authenticate(MainActivity.this); | |
} | |
}; | |
/** | |
* 執行登出的偵聽式 | |
* */ | |
private OnClickListener lisLogout = new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
try { | |
mEvernoteSession.logOut(MainActivity.this); | |
setLogin(); | |
} catch (InvalidAuthenticationException e) { | |
e.printStackTrace(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment