Android_SystemService
Posted Tears_fg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android_SystemService相关的知识,希望对你有一定的参考价值。
xml文件:
<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>
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_systemservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.GET_TASKS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
源代码:
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的主要内容,如果未能解决你的问题,请参考以下文章