问题描述
我需要我的应用程序可以给一个通知,每当无线脱机。
I need my application to give a notification whenever WiFi goes offline.
我得到它的每一次给一个通知WiFi连接的变化。但我需要时,它脱机它只能给一个通知。
I got it to give a notification every time the WiFi connection changes. But I need it to only give a notification when it goes offline.
此外,它提供了在启动(应用)的通知。
Also it gives a notification on start-up (of the application).
我的问题是,我该如何改变code当无线脱机只给一个通知?现在,它给出了一个通知,当它脱机,联机和启动工作。
My question is, how do I alter the code to only give a notification when WiFi goes offline? Now it gives a notification when it goes offline, online and on start-up.
在code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.registerReceiver(this.mConnReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
setContentView(R.layout.activity_main);
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if(currentNetworkInfo.isConnected()){
}else{
showNotification();
}
}
};
在此先感谢!
推荐答案
尝试这样的:
if(currentNetworkInfo != null &&
currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
currentNetworkInfo.getState() == NetworkInfo.State.DISCONNECTING) {
showNotification();
}
也有其他可能的状态(如 NetworkInfo.State.DISCONNECTED
,也许这就是你想要的),你可以在这里找到完整的列表:
There are also other possible states(such as NetworkInfo.State.DISCONNECTED
, maybe that is what you want), you can find the full list here:http://developer.android.com/reference/android/net/NetworkInfo.State.html
这篇关于通知每次断开连接的WiFi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!