1. 程式人生 > >獲取Android裝置資訊

獲取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();
		}
	}

效果:


三、獲取當前系統資訊


/**
	 * 獲取裝置型號、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();
		}

	}
五、獲取當前程式路徑和獲取已安裝的程式列表



/**
	 * 獲取當前程式路徑和獲取已安裝的程式列表
	 * 
	 * @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();
		}

	}

更詳細資訊可以下載