常用系统服务:
后台Service在系统启动时被SystemServer开启:
1.MountService:监听是否有SD卡安装及移除
2.ClipboardService:提供剪切板功能
3.PackageManagerService:提供软件包的安装移除及查看
4.电量,网络连接状态等等
使用的时候通过getSystemService()的方法来实现:
这是一个Activity的一个方法,通过传入name->object>服务对象
demo:注意添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/network"
android:onClick="doClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="判断网络是否连接" />
<Button
android:id="@+id/enableOrDisable_WIFI"
android:layout_width="wrap_content"
android:onClick="doClick"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/network"
android:layout_alignRight="@+id/network"
android:layout_below="@+id/network"
android:text="开关WIFI" />
<Button
android:id="@+id/getvoice"
android:onClick="doClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/enableOrDisable_WIFI"
android:layout_alignRight="@+id/enableOrDisable_WIFI"
android:layout_below="@+id/enableOrDisable_WIFI"
android:text="获取系统的音量" />
<Button
android:id="@+id/getPackagename"
android:onClick="doClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/getvoice"
android:layout_alignRight="@+id/getvoice"
android:layout_below="@+id/getvoice"
android:text="获取当前进程包名" />
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_main, null);
setContentView(view);
}
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, "当前运行的Activity包名:"+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;
}
}
演示效果: