1. 程式人生 > >Android 常用系統服務 學習總結

Android 常用系統服務 學習總結

                                                              清明小長假,小丸子浪的已經不知自己是一個要上班的孩子~來一張海邊遊妹子合照

好啦我不是要來寫旅遊傳記的啦,接下來就來進行常用系統服務的學習。

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