简体   繁体   English

Android Java从其他类访问MainActivity中的WebView

[英]Android Java access WebView in MainActivity from other class

I have public class MyFirebaseInstanceIdService to handle firebase notifications when the app is in the background. 我有public class MyFirebaseInstanceIdService ,可在应用程序在后台处理public class MyFirebaseInstanceIdService通知。

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        MainActivity.sendRegToken();
    }
}

MainActivity 主要活动

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    WebView webView = (WebView) findViewById(R.id.webView);

    public void sendRegToken() {
        String recent_token = FirebaseInstanceId.getInstance().getToken();
        webView.loadUrl("javascript:sendRegToken('"+recent_token+"');");
    }
}

I need access to webView so I can call loadUrl . 我需要访问webView因此可以调用loadUrl sendRegToken() is a non-static method, and I'm trying to access it from a static context. sendRegToken()是一种非静态方法,我正在尝试从静态上下文访问它。 I can't make sendRegToken() static because it contains the non-static webView . 我不能使sendRegToken()静态,因为它包含非静态的webView How do I get around this problem? 我该如何解决这个问题?

Use class LocalBroadcastManager Example can be found here : LocalBroadcastManager Example 1.register broadcast on the MainActivity. 使用类LocalBroadcastManager示例可以在这里找到: LocalBroadcastManager示例 1.在MainActivity上注册广播。 2.Send broadcast from onTokenRefesh() with parameters you need inside the Intent send. 2.使用onTokenRefesh()发送中需要的参数从onTokenRefesh()发送广播。

Please, do not do that. 请不要这样做。

If you have an API for this communication, just use normal Http Requests, if you don't have an API see this 如果您有用于此通信的API,请仅使用常规的Http请求,如果没有API,请参阅

By the way, Greenrobot/EventBus way better and simpler than LocalBroadCastManager. 顺便说一下,Greenrobot / EventBus比LocalBroadCastManager更好,更简单。

Disclaimer: Haven't tried, but in theory it should work... 免责声明:尚未尝试,但从理论上讲应该可以...

Use an Observer pattern. 使用观察者模式。

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    List<TokenRefreshListener> tokenListeners = new ArrayList<>();

    public void addTokenListener(TokenRefreshListener listener) {
       tokenListeners.add(listener);
   } 

  // Should also add a remove method

    @Override
    public void onTokenRefresh() {
        String token = getToken();
        for (TokenRefreshListener l:tokenListeners) {
            l.onTokenRefresh(token);
        } 
    }
}

And define that interface 并定义该接口

public interface TokenRefreshListener {
    void onTokenRefresh(String token);
} 

And in the Activity, you need to implement that interface 在“活动”中,您需要实现该接口

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, 
    TokenRefreshListener {

        WebView webView = (WebView) findViewById(R.id.webView);

     // need to call addTokenListener of the service 
     //... Probably do that in onStart method

     @Override
    public void onTokenRefresh(String token) {
        webView.loadUrl("javascript:sendRegToken('"+token+"');");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:从其他类访问MainActivity函数 - Android: Access a MainActivity function from other class class 从MainActivity类中的另一个函数访问WebView - Access to WebView from another function in MainActivity class 是否有从其他活动 class 访问 MainActivity 的解决方案? - Is there a solution that access MainActivity from other Activity class? 如何在Android Java中单击按钮时从MainActivity以外的类调用函数 - How to call a function from a class other than MainActivity on button click in Android Java Android / Java:如何从类引用MainActivity.java - Android/Java: How to reference from a class to the MainActivity.java 安卓 如何从另一个文件访问一个类到MainActivity - Android. How to access a class from another file to MainActivity 从MainActivity(Android)访问另一个类中的公共无效 - Access public void in another class from MainActivity (Android) Java Android Studio 无法将 ArrayList 从另一个 class 发送到 MainActivity - Java Android Studio cannot send ArrayList from another class to MainActivity 如何访问我在 MainActivity.java 中声明的微调器 object 到其他 java class - How to access spinner object which i have declared in MainActivity.java into other java class 从其他类创建的Access数据库(Android / JAVA) - Access Database created from other class (Android/JAVA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM