android Context

讲解Context的一篇不错的文章http://www.cppblog.com/life02/archive/2012/03/19/168328.aspx

 

调用service时需要一个Context实例!

1
2
3
4

    public void startWifi(Context context) {
        WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
    }

如果方法所在的类继承Activity,则上面代码可换为

1
2
3
4
5
6

    public void startWifi(Context context) {
        WifiManager wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
    }
posted @ 2012-09-07 17:23  helloweworld  阅读(175)  评论(0编辑  收藏  举报