码迷,mamicode.com
首页 > 移动开发 > 详细

Android_SystemService

时间:2016-07-31 17:24:23      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.android_systemservice.MainActivity" >
    <Button 
        android:id="@+id/network"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="判断网络是否连接"
        android:onClick="doClick"/>
    <Button 
        android:id="@+id/enableOrDisable_WIFI"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开关 WIFI"
        android:onClick="doClick"/>
    <Button 
        android:id="@+id/getVoice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取系统音量"
        android:onClick="doClick"/>
    <Button 
        android:id="@+id/getPackageName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取当前进程包名"
        android:onClick="doClick"/>
</LinearLayout>

main.java

package com.example.android_systemservice;

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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
/**
getSystemService是安卓很重要的一个api,它是Activity的一个方法,
根据传入的name来取得对应的Object,然后转换成相应的服务对象
*/
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        //利用系统服务获取布局
        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(isWorkConnected(MainActivity.this)){
                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 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.getPackageName:
            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;
            
        }
    }
    private boolean isWorkConnected(Context context) {
        // TODO Auto-generated method stub
        if(context != null){
            ConnectivityManager mConnectivityManager=(ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);//获取连接管理器对象
            NetworkInfo activeNetworkInfo = mConnectivityManager.getActiveNetworkInfo();//得到网络信息对象
            if(activeNetworkInfo != null )
                return activeNetworkInfo.isAvailable();//返回当前网络的状态
        }
        return false;
    }

}

 

Android_SystemService

标签:

原文地址:http://www.cnblogs.com/fangg/p/5723204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!