Android - Override getPackageName() in application and get component callback.

荒凉一梦 提交于 2020-01-15 10:39:26

问题


in my Android app that uses a webview component, I have extended the Application class and I override the getPackageName() method. This method is called multiple times in the life of my application by various component. I want to know if it's the webview component that call it. Here is my code:

package com.xxx.xxx;
import android.app.Application;

public class Global extends Application {

    private static Global instance; 

    public Global() { 
        instance = this; 
    } 

    private final String PACKAGE="com.webnews.appdirector";

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

    @Override
    public String getPackageName() {
        return PACKAGE;  //this work fine, but how to know if is webkit callback ??? "instance" have mcomponentcallbacks set to webkit, but how to read it ???
    }
}

Can you help me ?


回答1:


A little late to answer, but this should work.

@Override
public String getPackageName() 
{
    try 
    {
        throw new Exception();
    } 
    catch (Exception e)
    {
        StackTraceElement[] elements = e.getStackTrace();

        for (StackTraceElement element: elements) 
        {
            if(element.getClassName().startsWith("android.webkit."))
            {
                return null;
            }
        }
    }

    return super.getPackageName();
}


来源:https://stackoverflow.com/questions/36543687/android-override-getpackagename-in-application-and-get-component-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!