清明小长假,小丸子浪的已经不知自己是一个要上班的孩子~来一张海边游妹子合照
好啦我不是要来写旅游传记的啦,接下来就来进行常用系统服务的学习。
1、什么是SystemService?
Android的后台运行着很多的Service,它们在系统启动时被SystemServer开启,支持系统的正常工作,应用程序可以通过系统提供的Manager接口来访问这些Service提供的数据。我自己的理解就是系统为我们获取数据提供的一个渠道当然这也只是我的个人理解,不一定就是准确的。
2、如何使用SystemService?
其实很简单通过一个Activity对象去调用getSystemService方法并传入想要拿到的服务的name,系统就会自动返回给你一个Object对象。
代码示例:
<span style="font-size:14px;">LayoutInflater layoutInflater = (LayoutInflater) SystemServiceTestActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);</span>是不是超级简单呢?
3、那么常用的SystemService都有哪些呢?
打开Context.java的源码,你会发现这样一块代码:
<span style="font-size:14px;"> /** @hide */
@StringDef({
POWER_SERVICE,
WINDOW_SERVICE,
LAYOUT_INFLATER_SERVICE,
ACCOUNT_SERVICE,
ACTIVITY_SERVICE,
ALARM_SERVICE,
NOTIFICATION_SERVICE,
ACCESSIBILITY_SERVICE,
CAPTIONING_SERVICE,
KEYGUARD_SERVICE,
LOCATION_SERVICE,
//@hide: COUNTRY_DETECTOR,
SEARCH_SERVICE,
SENSOR_SERVICE,
STORAGE_SERVICE,
WALLPAPER_SERVICE,
VIBRATOR_SERVICE,
//@hide: STATUS_BAR_SERVICE,
CONNECTIVITY_SERVICE,
//@hide: UPDATE_LOCK_SERVICE,
//@hide: NETWORKMANAGEMENT_SERVICE,
NETWORK_STATS_SERVICE,
//@hide: NETWORK_POLICY_SERVICE,
WIFI_SERVICE,
WIFI_PASSPOINT_SERVICE,
WIFI_P2P_SERVICE,
WIFI_SCANNING_SERVICE,
//@hide: WIFI_RTT_SERVICE,
//@hide: ETHERNET_SERVICE,
WIFI_RTT_SERVICE,
NSD_SERVICE,
AUDIO_SERVICE,
FINGERPRINT_SERVICE,
MEDIA_ROUTER_SERVICE,
TELEPHONY_SERVICE,
TELEPHONY_SUBSCRIPTION_SERVICE,
CARRIER_CONFIG_SERVICE,
TELECOM_SERVICE,
CLIPBOARD_SERVICE,
INPUT_METHOD_SERVICE,
TEXT_SERVICES_MANAGER_SERVICE,
APPWIDGET_SERVICE,
//@hide: VOICE_INTERACTION_MANAGER_SERVICE,
//@hide: BACKUP_SERVICE,
DROPBOX_SERVICE,
//@hide: DEVICE_IDLE_CONTROLLER,
DEVICE_POLICY_SERVICE,
UI_MODE_SERVICE,
DOWNLOAD_SERVICE,
NFC_SERVICE,
BLUETOOTH_SERVICE,
//@hide: SIP_SERVICE,
USB_SERVICE,
LAUNCHER_APPS_SERVICE,
//@hide: SERIAL_SERVICE,
//@hide: HDMI_CONTROL_SERVICE,
INPUT_SERVICE,
DISPLAY_SERVICE,
USER_SERVICE,
RESTRICTIONS_SERVICE,
APP_OPS_SERVICE,
CAMERA_SERVICE,
PRINT_SERVICE,
CONSUMER_IR_SERVICE,
//@hide: TRUST_SERVICE,
TV_INPUT_SERVICE,
//@hide: NETWORK_SCORE_SERVICE,
USAGE_STATS_SERVICE,
MEDIA_SESSION_SERVICE,
BATTERY_SERVICE,
JOB_SCHEDULER_SERVICE,
//@hide: PERSISTENT_DATA_BLOCK_SERVICE,
MEDIA_PROJECTION_SERVICE,
MIDI_SERVICE,
RADIO_SERVICE,
})</span>
这应该是列举了所有的系统服务,那么我们平时比较常用的会有以下几个:
4、接下来我就用一个小demo来演示一下。
首先是布局:activity_system_service.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_adjust_network"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/network"/>
<Button
android:id="@+id/btn_wifi"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/wifi"/>
<Button
android:id="@+id/btn_voice"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/voice"/>
<Button
android:id="@+id/btn_package_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/package_name"/>
</LinearLayout></span>
实现的效果图如下:
接下来就是逻辑代码:SystemServiceTestActivity.java
<span style="font-size:14px;">public class SystemServiceTestActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater = (LayoutInflater) SystemServiceTestActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.activity_system_service, null);
setContentView(view);
findViewById(R.id.btn_adjust_network).setOnClickListener(this);
findViewById(R.id.btn_wifi).setOnClickListener(this);
findViewById(R.id.btn_voice).setOnClickListener(this);
findViewById(R.id.btn_package_name).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_adjust_network:
if (checkNetworkState(SystemServiceTestActivity.this)) {
Toast.makeText(SystemServiceTestActivity.this, "网络连接正常", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SystemServiceTestActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_wifi:
WifiManager wifiManager = (WifiManager) SystemServiceTestActivity.this.getSystemService(WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(SystemServiceTestActivity.this, "wifi已经关闭", Toast.LENGTH_SHORT).show();
} else {
wifiManager.setWifiEnabled(true);
Toast.makeText(SystemServiceTestActivity.this, "wifi已经打开", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_voice:
AudioManager audioManager = (AudioManager) SystemServiceTestActivity.this.getSystemService(AUDIO_SERVICE);
int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
int current = audioManager.getStreamVolume(AudioManager.STREAM_RING);
Toast.makeText(SystemServiceTestActivity.this, "系统最大音量为:" + max + ",系统当前音量为:" + current, Toast.LENGTH_SHORT).show();
break;
case R.id.btn_package_name:
ActivityManager activityManager = (ActivityManager) SystemServiceTestActivity.this.getSystemService(ACTIVITY_SERVICE);
String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
Toast.makeText(SystemServiceTestActivity.this, packageName, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
public boolean checkNetworkState(Context context) {
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
return networkInfo.isAvailable();
}
}
return false;
}
}
</span>
这里要注意的就是在恰当的时候一定要进行判空,否则就可能有空指针异常,其次就是当我们请求某一项服务时,可能需要权限,如果不清楚,运行之后根据错误提示添加即可。
今天总结就到这里,END