当前位置: 移动技术网 > IT编程>移动开发>Android > 如何给自己的app添加分享到有道云笔记这样的功能

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

2019年04月25日  | 移动技术网IT编程  | 我要评论

菲军公布中国扩建,山西电视台公民与法,卡奇娱乐荐藏片

文章同步自

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

问题:

在之前的一个开源笔记类项目中,有个用户反馈想增加类似分享到有道云笔记的功能,这样就可以把自己小米便签或者是其他记事本的内容分享到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与之对应,他来接收数据,接着就是获取到接收的数据,结合具体的业务逻辑做后续的处理,如保存到本地数据库,或者是展示在当前页面等。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网