通知每次WiFi是断开 [英] Notification everytime wifi is disconnected

查看:127
本文介绍了通知每次WiFi是断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的应用程序可以给一个通知,每当无线脱机。

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();}

 }
 };

在此先感谢!

推荐答案

使用的onReceive)这一行(广播接收器

if(!isNetworkConnectionAvailable(ctx)){
        Toast.makeText(ctx, "Network Connection Available ", Toast.LENGTH_LONG).show();
    }

在code代表isNetworkConnectionAvailable()是

the code for isNetworkConnectionAvailable() is

    public static boolean isNetworkConnectionAvailable(Context context)
{
    boolean isNetworkConnectionAvailable = false;

    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

    if(activeNetworkInfo != null) 
    {
        isNetworkConnectionAvailable = activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
    }
    return isNetworkConnectionAvailable;
}

发表评论我有关结果。

comment me about result

这篇关于通知每次WiFi是断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆