[英]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.