我怎么知道Glass连接到wifi并且电池正在充电

时间:2014-03-06 10:38:05

标签: google-glass google-gdk

我正在开发一些需要上传大量图像的东西,所以我认为当Glass连接到wifi和电池充电器时最好这样做。此外,如果它处于空闲模式,那将是最好的。

我可以在哪里放置图片上传代码以满足此要求?

1 个答案:

答案 0 :(得分:0)

如评论中所述,它应该与其他Android设备相同。

你可以试试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Card card = new Card(this);
    card.setText("wifi " + MainActivity.isWiFiConnected(this) + " charger " + MainActivity.isChargerConnected(this));        
    setContentView(card.toView());
    Log.v("TEST", "wifi " + MainActivity.isWiFiConnected(this));
    Log.v("TEST", "charger " + MainActivity.isChargerConnected(this));
    if(MainActivity.isWiFiConnected(this) && MainActivity.isChargerConnected(this)) { 
       // do whatever...
    }
}

public static boolean isWiFiConnected(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo.State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
        return true;
    return false;
}

public static boolean isChargerConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

添加

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
清单中的

另外,要检查Glass是否真正连接到互联网,请参阅How to check if Google Glass is connected to internet using GDK