zoukankan      html  css  js  c++  java
  • 如何给自己的app添加分享到有道云笔记这样的功能

    文章同步自http://javaexception.com/archives/34

    如何给自己的app添加分享到有道云笔记这样的功能

    问题:

    在之前的一个开源笔记类项目Leanote中,有个用户反馈想增加类似分享到有道云笔记的功能,这样就可以把自己小米便签或者是其他记事本的内容分享到Leanote中。

    解决办法:

    那么如何实现呢。需要有一个Activity来接受传递过来的内容,同时也需要在androidManifest.xml文件中配置。

    <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>

    接着我们需要考虑的是如何获取传递过来的内容。先提供一个处理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;
        }
    }

    获取分享的内容,并在当前页面展示

    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;
    }

    总结一下,就是需要在androidManifest.xml里面配置支持text/plain的特定intent-filter,然后有个Activity与之对应,他来接收数据,接着就是获取到接收的数据,结合具体的业务逻辑做后续的处理,如保存到本地数据库,或者是展示在当前页面等。

    看到了吧,这并没有想象中的那么难。

  • 相关阅读:
    MySQL 处理重复数据
    MySQL 序列使用
    MySQL 元数据
    MySQL 临时表和复制表
    MySQL 索引
    MySQL ALTER命令-修改数据表名或者修改数据表字段
    MySQL 事务
    MySQL 正则表达式
    MySQL NULL 值处理
    MySQL 排序
  • 原文地址:https://www.cnblogs.com/xing-star/p/10761018.html
Copyright © 2011-2022 走看看