Reputation: 4191
My need is that I have a broadcast receiver and an activity which has a webview. So when receiver receive some event I need to pass some arguments to the webivew or run some JS code. How to do it ?
I am not able to find a way to get access to the webview object in my main activity from receiver.
Upvotes: 1
Views: 1623
Reputation: 2358
You should be able to define a broadcast receiver as an inner class in your activity. In the same activity you should have a reference to your WebView that is accessible to the broadcast receiver.
public class MyActivity extends Activity {
WebView mWebview;
BroadcastReceiver mBroadcastReceiver;
@Override
protected void onCreate() {
...
mWebView = (WebView) findViewById(R.id.webview);
if (mBroadcastReceiver == null) {
mBroadcastReceiver = new MyBroadcastReceiver();
}
...
}
@Override
public void onResume() {
//register broadcast receiver
}
@Override
public void onPause() {
//unregister broadcast receiver
}
class MyBroadcastReceiver extends BroadcastReceiver {
@Override
onReceive(Intent intent) {
//Receive intent here
//Call instance of webview
mWebView.doSomething();
}
}
}
Also, you'll need to register your broadcast receiver dynamically in the onResume() method of your activity and unregister it in the onPause() method.
Upvotes: 2