如何检查是否启用无线/ 3G? [英] How to check if wifi/3g is enabled?

查看:142
本文介绍了如何检查是否启用无线/ 3G?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,presents一些RSS新闻。我的问题是,我想present举杯消息没有互联网连接如果WiFi被禁用或没有3G连接。我必须作出如果WiFi关闭,以弹出消息它的工作,但我的问题是:


  

我希望应用程序来检查用户是否启用WiFi或3G连接。如果是3G进入新的活动,如果如果已启用,但如果用户连接到一个网络,然后去新的活动有WIFI不仅检查。如果3G或在WiFi被禁用,我只想present我以前写的消息。


这是code我写的,但现在我真的不知道该怎么继续......

清单:

 <使用许可权的android:NAME =android.permission.ACCESS_WIFI_STATE>< /使用许可权>
<使用许可权的android:NAME =android.permission.UPDATE_DEVICE_STATS>< /使用许可权>
<使用许可权的android:NAME =android.permission.CHANGE_WIFI_STATE>< /使用许可权>
<使用许可权的android:NAME =android.permission.WAKE_LOCK>< /使用许可权>

和我的的onCreate 法源:

  @覆盖
公共无效的onCreate(捆绑冰柱)
{
        super.onCreate(冰柱);
        的setContentView(R.layout.main2);        wifiManager =(WifiManager)this.getSystemService(Context.WIFI_SERVICE);        如果(wifiManager.isWifiEnabled())
        {            wifiManager.setWifiEnabled(真); //一个欸乃anoixto ASTO anoixto
        }
        其他
        {            Toast.makeText(nea.this,Χρειάζεστεσύνδεσηστο互联网,Toast.LENGTH_SHORT).show();
            完();
        }        loadFeed(ParserType.DOM);
    }


解决方案

您应该present该消息是否有根据您所使用的HTTP API(HttpClient的为例)是一个IOException或类似。这样,你就不会做不必要的额外通话到无线管理器及相关服务。

如果不能获取的RSS,则该用户不具有的数据服务(或者无线或3G)。

I have a button that presents some RSS news. My problem is that I would like to present a toast message "there is no internet connection" if the wifi is disabled or there is no 3g connection. I have to make it work if the wifi is closed to pop out the message but my problem is:

I want the application to check if the user has wifi enabled or 3g connection. If there is 3g to go the new activity, if there is wifi to check not only if it is enabled but if the user is connected to a network and then go to the new activity. If the 3g or the wifi is disabled, I just want to present the message I wrote before.

This is the code I have written as now but I don't really know how to continue...

The manifest:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

And the source for my onCreate method:

@Override 
public void onCreate(Bundle icicle) 
{ 
        super.onCreate(icicle);
        setContentView(R.layout.main2);

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        if (wifiManager.isWifiEnabled()) 
        {

            wifiManager.setWifiEnabled(true); // an einai anoixto asto anoixto
        } 
        else 
        {

            Toast.makeText(nea.this, "Χρειάζεστε σύνδεση στο internet", Toast.LENGTH_SHORT).show();
            finish();
        }

        loadFeed(ParserType.DOM);
    }

解决方案

You should present that message if there is an IOException or similar according to the HTTP API you're using (HttpClient for example). This way you won't make unnecessary extra-calls to Wifi manager and related services.

If you can't fetch the RSS, then the user doesn't have a data service (either Wifi or 3G).

这篇关于如何检查是否启用无线/ 3G?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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