后台Service在系统启动时被SystemService开启
MountService:监听是否有SD卡安装及移除
ClipboardService:提供剪切板功能
PackageManagerService:提供软件包的安装移除及查看
电量、网络连接状态等
getSystemService()
Activity的一个方法
传入NAME -> Object -> 服务对象
常用系统服务
判断网络是否连接
开关WIFI
获取当前的系统音量
获取当前运行的程序包名
布局填充器
通知管理器
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
获取网络状态
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; default: 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; }
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
打开或关闭WIFI
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 : " + max + " current : " + current, Toast.LENGTH_SHORT).show(); break;
获取包名
<uses-permission android:name="android.permission.GET_TASKS"/>
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, "当前运行的Activity包名 : " + packageName, Toast.LENGTH_SHORT).show(); break;