• 如何给自己的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与之对应,他来接收数据,接着就是获取到接收的数据,结合具体的业务逻辑做后续的处理,如保存到本地数据库,或者是展示在当前页面等。

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

  • 相关阅读:
    [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面
    flex调用webservice中的datatable结果写入datagrid
    linux shell编程指南第十一章------------合并与分割2
    Java GUI使用exe4j打包exe文件
    最小生成树kruskal算法
    JQuery Datatable Ajax请求两次问题的解决
    其实我还好
    EFM32在使用IAR开发环境配置ICf文件以及指定程序存储地址空间
    hdu1151Air Raid
    UIWebview打开.txt文件中文乱码解决
  • 原文地址:https://www.cnblogs.com/xing-star/p/10761018.html
Copyright © 2020-2023  润新知