Android系统服务简介

时间:2022-02-01 06:32:14

1、getSystemService();

Activtyde 一个方法(传入name)。

Android系统服务简介Android系统服务简介

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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubWifiManager 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();}}});