问题
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