Android系统服务简介

1、getSystemService();

Activtyde 一个方法(传入name)。

Android系统服务简介_第1张图片Android系统服务简介_第2张图片

2、简单使用:

(1)绑定布局:

setContentView(R.layout.activity_main);
LayoutInflater inflater  = (LayoutInflater )  MainActivity.this.getSysytemService(LAYOUT_INFLATER_SERVICE);  
View view = inflater.inflater(R.layout.activity_main)
setContentView(view);

(2)查看网络状态:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

public boolean isNetWordConnected(Context context){
		if(context!=null){
			ConnectivityManager connectivityManager=(ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();  
			if(networkInfo!=null){
				return networkInfo.isAvailable();   //活动状态
			}
		}
		return false;
	}
(3)WiFi服务
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
//Wifi服务
		findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(WIFI_SERVICE);
				if(wifiManager.isWifiEnabled()){
					wifiManager.setWifiEnabled(false);
					Toast.makeText(MainActivity.this, "Wifi已经关闭", Toast.LENGTH_LONG).show();
				}else{
					wifiManager.setWifiEnabled(true);
					Toast.makeText(MainActivity.this, "Wifi已经打开", Toast.LENGTH_LONG).show();	
				}
			}
		});

你可能感兴趣的:(Android开发,系统服务)