android - 如何在自己的进程中使用库向 Android 应用程序添加分析

标签 android google-analytics multiprocessing

我有一个使用 Google Analytics 的 Android 应用程序。如recommended by Google ,它会覆盖 android.app.Application并在其 AndroidManifest.xml 中提供类名:

<application
    android:name=".MyApplication">

我的应用程序依赖于一个库,该库包含必须在自己的进程中运行的服务。因此,其AndroidManifest.xml拥有:
<application>
    <service
        android:name="org.myservice"
        android:enabled="true"
        android:exported="false"
        android:process=":myservice">

运行时,我发现 MyApplication 的第二个独立实例在图书馆的进程中启动。这试图设置分析等。但图书馆从来没有要求过这个类,也不想要它。

我试图在库的 AndroidManifest.xml 中声明一个不同的应用程序类。但这会导致“ list 合并失败”构建错误。

如何使库进程不依赖于 MyApplication ?

最佳答案

因为 list 是合并在一起的,所以只能有一个由 android:name 指定的类。 (在一个库或主应用程序中)。这些对象之一将在每个进程中实例化。

我的解决方案是让 Main 应用程序类检测进程名称是否与 APPLICATION_ID 匹配。这仅适用于主进程,而不适用于任何其他进程。

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // The print library has its own process but will use this class.
        // Only set up analytics if we are in the main process.
        if (isMainProcess()) {
            // Once-per-app code here
        }
    }

    public boolean isMainProcess() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return TextUtils.equals(BuildConfig.APPLICATION_ID,
                        processInfo.processName);
            }
        }
        return false;
    }
}

关于android - 如何在自己的进程中使用库向 Android 应用程序添加分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35608649/

相关文章:

java - android:ExecutorService:- ExecutorService 不等待 executorService 完成

google-analytics - 从谷歌分析中删除我的本地访问

python - multiprocessing.Pool apply_async 中的 error_callback 在 Python 2 中?

python - 多处理的queue.get()什么时候返回DONE?

Android 应用程序在后台

android - 检测 Intent 的类型 android

java - 读取来电号码

.htaccess - 使用 .htaccess 重定向我的附属链接并在 Google Analytics 中跟踪

android - 将 Google Analytics 添加到我的应用程序时出现 XML 错误

python - 多处理不重新创建对象