7 Android中常用的系統服務

weixin_30924079發表於2020-04-04

7-1 系統服務概述

7-2 使用系統服務(一) 

<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="doClick"
        android:text="判斷網路是否連線" />

    <Button
        android:id="@+id/enableOrDisable_WIFI"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/network"
        android:layout_alignRight="@+id/network"
        android:layout_below="@+id/network"
        android:onClick="doClick"
        android:text="開關WIFI" />

    <Button
        android:id="@+id/getvoice"
        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:onClick="doClick"
        android:text="獲取系統的音量" />

    <Button
        android:id="@+id/getPackagename"
        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:onClick="doClick"
        android:text="獲取當前程式包名" />

</RelativeLayout>

1.判斷網路是否連線

MainActivity.java

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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);
        // 1. 使用LayoutInflater把一個layout轉換成view
        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_main, null);
        setContentView(view);
        // setContentView(R.layout.activity_main);相當於上面的程式碼
    }

    public void doClick(View v) {
        switch (v.getId()) {
        case R.id.network:
            if (isNetWorkConnetion(MainActivity.this) == true) {
                Toast.makeText(MainActivity.this, "網路已經開啟", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "網路未連線", Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }

    // 判斷網路的方法
    public boolean isNetWorkConnetion(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mNetWorkInfo = mConnectivityManager.getActiveNetworkInfo();// 獲取網路連線狀態資訊
            if (mNetWorkInfo != null) {// 如果不為空
                return mNetWorkInfo.isAvailable();// 返回網路狀態屬於活動狀態
            }
        }
        return false;
    }
}

 

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

開關WIFI

package com.example.test;

import android.app.Activity;
import android.content.Context;
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);
        // 1. 使用LayoutInflater把一個layout轉換成view
        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_main, null);
        setContentView(view);
        // setContentView(R.layout.activity_main);相當於上面的程式碼
    }

    public void doClick(View v) {
        switch (v.getId()) {
        case R.id.network:
            if (isNetWorkConnetion(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;
        }
    }

    // 判斷網路的方法
    public boolean isNetWorkConnetion(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mNetWorkInfo = mConnectivityManager.getActiveNetworkInfo();// 獲取網路連線狀態資訊
            if (mNetWorkInfo != null) {// 如果不為空
                return mNetWorkInfo.isAvailable();// 返回網路狀態屬於活動狀態
            }
        }
        return false;
    }
}

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

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

7-3 使用系統服務(二)

獲取系統音量

package com.example.test;

import android.app.Activity;
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);
        // 1. 使用LayoutInflater把一個layout轉換成view
        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_main, null);
        setContentView(view);
        // setContentView(R.layout.activity_main);相當於上面的程式碼
    }

    public void doClick(View v) {
        switch (v.getId()) {
        case R.id.network:
            if (isNetWorkConnetion(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;
        }
    }

    // 判斷網路的方法
    public boolean isNetWorkConnetion(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mNetWorkInfo = mConnectivityManager.getActiveNetworkInfo();// 獲取網路連線狀態資訊
            if (mNetWorkInfo != null) {// 如果不為空
                return mNetWorkInfo.isAvailable();// 返回網路狀態屬於活動狀態
            }
        }
        return false;
    }
}

     

獲取當前程式包名   

package com.example.test;

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);
        // 1. 使用LayoutInflater把一個layout轉換成view
        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_main, null);
        setContentView(view);
        // setContentView(R.layout.activity_main);相當於上面的程式碼
    }

    public void doClick(View v) {
        switch (v.getId()) {
        case R.id.network:
            if (isNetWorkConnetion(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 isNetWorkConnetion(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mNetWorkInfo = mConnectivityManager.getActiveNetworkInfo();// 獲取網路連線狀態資訊
            if (mNetWorkInfo != null) {// 如果不為空
                return mNetWorkInfo.isAvailable();// 返回網路狀態屬於活動狀態
            }
        }
        return false;
    }
}

 

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

 

轉載於:https://www.cnblogs.com/crazyzx/articles/5350900.html

相關文章