采用了android:工艺=":远程"重新创建Android应用程序对象 [英] using android:process=":remote" recreates android Application object

查看:120
本文介绍了采用了android:工艺=":远程"重新创建Android应用程序对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用AIDL服务。 我也想运行另一个进程,所以我用在服务声明清单机器人::工艺=远程。 我的问题是,当:遥控器进程开始它显然重新创建应用程序对象。 我真的不与我重写应用程序对象,并调用大量客户端的东西在的onCreate()方法。然而,我希望该服务code驻留在同一个APK与客户端。

I'm using AIDL service in my app. I also want to run it another process, so I use 'android:process=":remote"' in service declaration in the manifest. My problem is that when the ':remote' process starts it apparently recreates Application object. I really do not with that as I override application object and call lots of client stuff in the 'onCreate()' method. Yet I want the service code to reside in the same apk with the client.

我可以做到这一点?是当新进程启动应用程序对象总是重现? 鸭preciate你的帮助。谢谢!

Can I achieve that? Is Application object always recreated when new process starts? Appreciate your help. Thanks!

推荐答案

正如前面提到的 CommonsWare ,每个过程的都有自己的应用程序对象。

As already mentioned by CommonsWare, each of the processes gets its own Application object.

在你的 Application.onCreate()方法,你可以检查是否该方法正在从主流程中或调用的远程进程中,并相应地初始化不同的东西。

In your Application.onCreate() method you can check whether the method is being called from within the main process or from within the remote process and initialize different stuff accordingly.

@Override
public void onCreate()
{
    super.onCreate();

    if(isRemoteProcess(this))
    {
        // initialize remote process stuff here
    }
    else
    {
        // initialize main process stuff here
    }
}

private boolean isRemoteProcess(Context context)
{
    Context applicationContext = context.getApplicationContext();
    long myPid = (long) Process.myPid();
    List<RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) applicationContext.getSystemService("activity")).getRunningAppProcesses();
    if (runningAppProcesses != null && runningAppProcesses.size() != 0)
    {
        for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses)
        {
            if (((long) runningAppProcessInfo.pid) == myPid && "YOUR_PACKAGE_NAME:remote".equals(runningAppProcessInfo.processName))
            {
                return true;
            }
        }
    }
    return false;
}

这篇关于采用了android:工艺=&QUOT;:远程&QUOT;重新创建Android应用程序对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆