My Class NetworkListener应检查Internet连接是否可用。然后,它应该显示一个带有InternetState的Snackbar。为此,我正在使用一个界面。
我的整个代码:
[MainActivity.java】:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle(R.string.Inbox);
Initialize_NavigationDrawer();
Initialize_Objects();
//1. Here, I initialise my NetworkListener and register my Receiver
NetworkListener mNetworkListener = new NetworkListener();
mNetworkListener.registerReceiver(this);
}
@Override
public void ToggleSnackbar(boolean ShowHide) {
if (ShowHide) snbNoInternet.show();
else snbNoInternet.dismiss();
}
[NetworkListener.java]:
public class NetworkListener extends BroadcastReceiver {
private SnackbarInterface snbInterface;
public NetworkListener() {}
@Override
public void onReceive(Context context, Intent intent) {
//4. After toggling the Network-Connection, a NullReferenceException occurs
//5. System.out.println(snbInterface) shows: null
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null || !ni.isConnected()) {
snbInterface.ToggleSnackbar(true);
}
else if (ni != null && ni.isConnected()) {
snbInterface.ToggleSnackbar(false);
}
}
public void registerReceiver(SnackbarInterface receiver) {
snbInterface = receiver; //2. Is called from onCreate
//3. System.out.println(snbInterface) shows: at.guger.email.activities.MainActivity@27472a5e
}
public interface SnackbarInterface {
public void ToggleSnackbar(boolean ShowHide);
}
}
[AndroidManifest.xml中]:
<activity
android:name=".activities.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.WriteActivity" />
<activity
android:name=".activities.ReadActivity" />
<receiver
android:name=".NetworkListener">
<intent-filter>
<action
android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
和权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
调用代码就像订购号码一样! 我真的无法解释,为什么snbInterface设置为null!
我希望你能帮助我!
答案 0 :(得分:0)
您使用AndroidManifest中的BroadcastReceiver
元素注册了receiver
。 Android系统创建BroadcastReceiver实例并使用适当的参数调用onReceive
方法。
如果您想使用BroadcastReceiver
操纵应用程序的UI(例如显示/隐藏Snackbar
),则必须使用registerReceiver(BroadcastReceiver receiver,IntentFilter intentFilter)
在您的活动中注册接收器您活动的onCreate
方法中的unRegister
方法和onDestroy
。
从AndroidManifest.xml文件中删除receiver
元素,并通过代码创建BroadcastReceiver。
这里的这个问题清楚地解释了如何创建和注册BroadcastReceivers: Broadcast Receiver class and registerReceiver method
在BroadcastReceiver的onReceive方法中,调用Activity中的toggleSnackbar
方法来切换Snackbar。
还有另一种方法可以达到你想要的效果。这里讨论: Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
详细了解BroadcastReceivers here
您可以将Receiver编写为单独的类。实例化Receiver并使用第一个参数作为Receiver并将第二个参数作为IntentFilter调用registerReceiver
。