获取Android设备信息

在开发的过程中有可能根据需要显示一些设备的状态或系统信息。下面是收集到的一些信息

一、获取当前电量

每当电量改变时系统都会广播发Intent.ACTION_BATTERY_CHANGED消息出来,应用程序只需要注册并创建一个接收器就能获取当前的电量

创建一个广播接收器类BatteryInfoReceiver.java

package com.example.getdeviceinfo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

//接收电池改变的消息
public class BatteryInfoReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		int level = intent.getIntExtra("level", Integer.MAX_VALUE);
		Toast.makeText(context, String.valueOf(level) + "%", Toast.LENGTH_SHORT)
				.show();
	}

}

当点击按钮时注册Intent.ACTION_BATTERY_CHANGED消息

package com.example.getdeviceinfo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button batteryBtn = null;
	private BroadcastReceiver batteryReceiver = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		batteryReceiver = new BatteryInfoReceiver();
		InitBtn();
		batteryBtn.setOnClickListener(new batteryListener());
	}

	private void InitBtn() {
		batteryBtn = (Button) findViewById(R.id.battery);
		wifiBtn = (Button) findViewById(R.id.wifi);
	}

	class batteryListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//注册消息
			registerReceiver(batteryReceiver, new IntentFilter(
					Intent.ACTION_BATTERY_CHANGED));
		}
	}

}



二、获取Wifi的状态,只要用到WifiManager类

按钮响应函数代码

class wifiListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			WifiManager wifi = (WifiManager) MainActivity.this
					.getSystemService(Context.WIFI_SERVICE);
			String wifiState = null;
			int state = wifi.getWifiState();
			switch (state) {
			case WifiManager.WIFI_STATE_DISABLED:
				wifiState = "已经关闭";
				break;
			case WifiManager.WIFI_STATE_DISABLING:
				wifiState = "正在关闭...";
				break;
			case WifiManager.WIFI_STATE_ENABLED:
				wifiState = "已经打开";
				break;
			case WifiManager.WIFI_STATE_ENABLING:
				wifiState = "正在打开...";
				break;
			case WifiManager.WIFI_STATE_UNKNOWN:
				wifiState = "未知状态";
				break;
			default:
				break;
			}
			Toast.makeText(MainActivity.this, "获取wifi " + wifiState,
					Toast.LENGTH_SHORT).show();
		}
	}

效果:

获取Android设备信息_第1张图片


三、获取当前系统信息

获取Android设备信息_第2张图片


/**
	 * 获取设备型号、SDK版本及系统版本
	 * 
	 * @author Administrator
	 * 
	 */
	class androidInfoListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String device_model = Build.MODEL; // 设备型号
			int version_sdk = Build.VERSION.SDK_INT; // 设备SDK版本
			String version_release = Build.VERSION.RELEASE; // 设备的系统版本
			String str = "device_model = " + device_model + "  version_sdk = "
					+ version_sdk + "  version_release = " + version_release;
			Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT)
					.show();
		}

	}


四、获取屏幕宽高


/**
	 * 获取屏幕宽高
	 * 
	 * @author Administrator
	 * 
	 */
	class screenInfoListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			DisplayMetrics displayMetrics = new DisplayMetrics();
			MainActivity.this.getWindowManager().getDefaultDisplay()
					.getMetrics(displayMetrics);
			int height = displayMetrics.heightPixels;
			int width = displayMetrics.widthPixels;
			double diagonalPixels = Math.sqrt(Math.pow(
					displayMetrics.widthPixels, 2)
					+ Math.pow(displayMetrics.heightPixels, 2));
			double pixels = diagonalPixels / (160 * displayMetrics.density);
			String str = "height = " + height + "  width = " + width
					+ " pixels = " + pixels;
			Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT)
					.show();
		}

	}


五、获取当前程序路径和获取已安装的程序列表

获取Android设备信息_第3张图片

/**
	 * 获取当前程序路径和获取已安装的程序列表
	 * 
	 * @author Administrator
	 * 
	 */
	class pathListListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String strPath = getApplicationContext().getFilesDir()
					.getAbsolutePath();
			List<PackageInfo> packageInfoList = getPackageManager()
					.getInstalledPackages(0);
			Toast.makeText(getApplicationContext(),
					strPath + ":::" + packageInfoList.toString(),
					Toast.LENGTH_SHORT).show();
		}

	}

更详细信息可以下载

点击打开链接



你可能感兴趣的:(android,设备,电池)