How to add and share the function of Youdao cloud notes to your app

Keywords: Android xml Database

Article synchronization from http://javaexception.com/archives/34

How to add and share the function of Youdao cloud notes to your app

Question:

An open source notebook project before Leanote In, a user feedback wants to add the function of sharing to Youdao cloud notes, so that they can share their millet notes or other Notepad contents to the leavenote.

terms of settlement:

So how to achieve it. There needs to be an Activity to accept the passed content, and it also needs to be configured in the Android manifest.xml file.

<activity
android:name=".ui.edit.NoteEditActivity"
android:screenOrientation="portrait"
android:configChanges="uiMode|keyboard|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>

Then we need to consider how to get the content passed. First, provide a tool class to handle the content in Intent.

/**
 * Utilities for creating a share intent
 */
public class ShareUtils {
 
    /**
     * Create intent with subject and body
     *
     * @param subject
     * @param body
     * @return intent
     */
    public static Intent create(final CharSequence subject,
                                final CharSequence body) {
        Intent intent = new Intent(ACTION_SEND);
        intent.setType("text/plain");
        if (!TextUtils.isEmpty(subject))
            intent.putExtra(EXTRA_SUBJECT, subject);
        intent.putExtra(EXTRA_TEXT, body);
        return intent;
    }
 
    /**
     * Get body from intent
     *
     * @param intent
     * @return body
     */
    public static String getBody(final Intent intent) {
        return intent != null ? intent.getStringExtra(EXTRA_TEXT) : null;
    }
 
    /**
     * Get subject from intent
     *
     * @param intent
     * @return subject
     */
    public static String getSubject(final Intent intent) {
        return intent != null ? intent.getStringExtra(EXTRA_SUBJECT) : null;
    }
}

 

Get the shared content and show it on the current page

public Note getNoteFromShareIntent() {
    Note newNote = new Note();
    Account account = Account.getCurrent();
    newNote.setUserId(account.getUserId());
    newNote.setTitle(ShareUtils.getSubject(getIntent()));
    newNote.setContent(ShareUtils.getBody(getIntent()));
    Notebook notebook;
    notebook = NotebookDataStore.getRecentNoteBook(account.getUserId());
    if (notebook != null) {
        newNote.setNoteBookId(notebook.getNotebookId());
    } else {
        Exception exception = new IllegalStateException("notebook is null");
        CrashReport.postCatchedException(exception);
    }
    newNote.setIsMarkDown(account.getDefaultEditor() == Account.EDITOR_MARKDOWN);
    newNote.save();
    return newNote;
}

To sum up, it is necessary to configure a specific intent filter supporting text/plain in Android manifest.xml, and then have an Activity corresponding to it to receive the data, and then obtain the received data, combined with the specific business logic for subsequent processing, such as saving to the local database, or displaying on the current page.

See, it's not as hard as you think.

Posted by conker87 on Sat, 23 Nov 2019 09:06:22 -0800