在mainactivity中声明接收器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在mainactivity中声明接收器相关的知识,希望对你有一定的参考价值。
我有一个像这样声明的webView
(MainActivity.java)
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity
{
public static WebView webView;
WebView webView = (WebView) findViewById(R.id.webview);
webView.postUrl(url, postData.getBytes());
}
接收器处理连接更改(BroadcastReceiver.java)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionBroadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
MainActivity.webView.reload();
} else {
MainActivity.webView.reload();
}
}
}
清单(AndroidManifest.xml)
<application
android:allowBackup="true"
android:icon="@drawable/logo_full"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".ConnectionBroadReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
因为“webView”是一个“公共静态WebView webView”;我不能从另一个类调用虚方法..
onBroadReceiver:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.webkit.WebView.reload()'
我能怎么做 ?
答案
而不是在receiver
中注册manifest
,在Activity
中注册它并通过interface
进行网络状态变化的交互
network callback.Java
interface NetworkCallback{
void onStateChange();
}
connection broad receiver.Java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionBroadReceiver extends BroadcastReceiver {
private NetworkCallback callback;
public ConnectionBroadReceiver(NetworkCallback callback){
this.callback = callback;
}
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
callback.onStateChange();
} else {
callback.onStateChange();
}
}
}
现在在您的活动中
public class MainActivity extends AppCompatActivity
{
private WebView webView;
public void onCreate(Bundle savedInstanceState){
// you code to set content view
webView = (WebView) findViewById(R.id.webview);
webView.postUrl(url, postData.getBytes());
registerReceiver(new ConnectionBroadReceiver(new NetworkCallback(){
@Override
public void onStateChange(){
webview.reload();
}
}),new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
}
以上是关于在mainactivity中声明接收器的主要内容,如果未能解决你的问题,请参考以下文章
如何将片段活动添加到 MainActivity.java 或 MainActivity.kt
我想从片段中隐藏片段容器视图(在 MainActivity 布局内),但是当我单击任务按钮然后重新打开应用程序时它不起作用