前言
前段时间在写一个特性,需要在 native 层将信息持久化到 dropbox 当中。但是由于在 Android N 上,dropbox 相关的 client 端和 server 端都是由 Java 来实现的,在 native 层并没有相关的代理,因此我们不能在 native 层借助普通的 C++ 调用来实现这个功能,只能调用相关的 Java 方法来实现。
本篇文章就来介绍一下 C++ 如何调用 Java 方法,以及上面这个功能如何实现。(基于 Android N)
一、思路
1. DropBoxManager
将信息添加到 dropbox,需要借助于 DropBoxManager:
frameworks/base/core/java/android/os/DropBoxManager.java
/**
* Stores human-readable text. The data may be discarded eventually (or even
* immediately) if space is limited, or ignored entirely if the tag has been
* blocked (see {@link #isTagEnabled}).
*
* @param tag describing the type of entry being stored
* @param data value to store
*/
public void addText(String tag, String data) {
try {
mService.add(new Entry(tag, 0, data));
} catch (RemoteException e) {
if (e instanceof TransactionTooLargeException
&& mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
Log.e(TAG, "App sent too much data, so it was ignored", e);
return;
}