Android SystemService初体验

由于系统服务比较多就挑了几个试了一下,但是数据开关的还没有找到合适的方法后续再补上
这里写图片描述
使用到服务的时候别忘记在配置文件中添加相应的权限
MainActivity.class

package com.superxingyun.systemservicedemo;

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.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private ConnectivityManager connManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_main, null);
        setContentView(view);

    }

    public void doClick(View view) {
        switch (view.getId()){
            case R.id.network:
                if (isNetwork(MainActivity.this) == true){//判断网络状态返回值打吐司
                    Toast.makeText(MainActivity.this, "网络已连接", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "网络未连接", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.fouthG:

                break;
            case R.id.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.voice:
                AudioManager audioManager = (AudioManager) MainActivity.this.getSystemService(AUDIO_SERVICE);
                int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
                int current = audioManager.getStreamVolume(AudioManager.STREAM_RING);
                Toast.makeText(MainActivity.this, "最大音量" + max + "当前音量" + current, Toast.LENGTH_SHORT).show();
                break;
            case R.id.packagename:
                ActivityManager activityManager = (ActivityManager) MainActivity.this.getSystemService(ACTIVITY_SERVICE);
                String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
                Toast.makeText(MainActivity.this, "当前应用程序包名:" + packageName, Toast.LENGTH_SHORT).show();
                break;
        }
    }

    //获取网络状态 判断是否已连接网络
    public boolean isNetwork(Context context){
        if (context != null){
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            if (networkInfo != null){
                return manager.isActiveNetworkMetered();
            }
        }
        return false;
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.superxingyun.systemservicedemo">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name ="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局文件
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.superxingyun.systemservicedemo.MainActivity">

    <Button
        android:onClick="doClick"
        android:id="@+id/network"
        android:layout_width="match_parent"
        android:text="@string/network"
        android:layout_height="wrap_content" />
    <Button
        android:onClick="doClick"
        android:id="@+id/fouthG"
        android:layout_width="match_parent"
        android:text="@string/_4g"
        android:layout_height="wrap_content"
        android:layout_below="@+id/network"/>
    <Button
        android:onClick="doClick"
        android:id="@+id/wifi"
        android:layout_width="match_parent"
        android:text="@string/wifi"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fouthG"/>
    <Button
        android:onClick="doClick"
        android:id="@+id/voice"
        android:layout_width="match_parent"
        android:text="@string/getvoice"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wifi"/>
    <Button
        android:onClick="doClick"
        android:id="@+id/packagename"
        android:layout_width="match_parent"
        android:text="@string/packagename"
        android:layout_height="wrap_content"
        android:layout_below="@+id/voice"/>
</RelativeLayout>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值