public void doClick(View v) { switch (v.getId()) { case R.id.network: if (isNetWorkConnected(MainActivity.this) == true) { Toast.makeText(MainActivity.this, "有网络连接", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(MainActivity.this, "网络未连接", Toast.LENGTH_SHORT) .show(); } break; case R.id.enableOrDisable_WIFI: WifiManager wifiManager = (WifiManager) MainActivity.this .getSystemService(WIFI_SERVICE); if (wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(false); Toast.makeText(MainActivity.this, "Wifi已关闭", Toast.LENGTH_SHORT) .show(); } else { wifiManager.setWifiEnabled(true); Toast.makeText(MainActivity.this, "Wifi已打开", Toast.LENGTH_SHORT) .show(); } break; case R.id.getvoice: AudioManager mAudioManager = (AudioManager) MainActivity.this .getSystemService(AUDIO_SERVICE); int max = mAudioManager .getStreamMaxVolume(AudioManager.STREAM_SYSTEM); int current = mAudioManager .getStreamVolume(AudioManager.STREAM_RING); Toast.makeText(MainActivity.this, "最大音量为:" + max + ",当前音量为:" + current, Toast.LENGTH_SHORT) .show(); break; case R.id.getPackagename: ActivityManager activityManager = (ActivityManager) MainActivity.this .getSystemService(ACTIVITY_SERVICE); String packageName = activityManager.getRunningTasks(1).get(0).topActivity .getPackageName(); Toast.makeText(MainActivity.this, "当前的包名为:\n" + packageName, Toast.LENGTH_SHORT).show(); break; } }
public boolean isNetWorkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(CONNECTIVITY_SERVICE); // 获取当前网络活动状态 NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable();// 返回是否有网络链接 } } return false; }
ziom
帅帅嗒先锋
JustWannaHugU