Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中常用開發工具類—持續更新...

Android中常用開發工具類—持續更新...

編輯:關於Android編程

一、自定義ActionBar

 

public class ActionBarTool {
	public static void setActionBarLayout(Activity act,Context context,int layoutId){//前兩個參數一般情況下都可以填this,第三個參數為自定義View的Id
	    ActionBar actionBar = act.getActionBar();
	    if( null != actionBar ){
	        actionBar.setDisplayShowHomeEnabled(false);//是否顯示左上角的應用圖標
	        actionBar.setDisplayShowCustomEnabled(true);//是否使用自定義標題欄
	        LayoutInflater inflator = (LayoutInflater)   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        View v = inflator.inflate(layoutId, null);//填充自定義布局
	        ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	        actionBar.setCustomView(v,layout);//把自定義View設置成ActionBar
	    }
	}
}

 

二、系統信息工具類

 

public class SystemInfoUtils {
	public static int getRunningAppProcesses(Context context) {// 得到當前系統內運行的app進程數量
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List appInfos = am.getRunningAppProcesses();
		return appInfos.size();
	}

	public static List> getAppInfo(Context context) {// 得到非系統應用的app信息
		List> appInfos = new ArrayList>();
		HashMap map = null;
		List packages = context.getPackageManager()
				.getInstalledPackages(0);// 參數傳入0表示過濾權限,因為可能有的手機不讓你獲取應用信息
		for (PackageInfo temp : packages) {
			if ((temp.applicationInfo.flags & temp.applicationInfo.FLAG_SYSTEM) == 0) {
				// 非系統應用
				// 得到應用的名稱
				String appName = temp.applicationInfo.loadLabel(
						context.getPackageManager()).toString();
				// 得到應用圖標
				Drawable appIcon = temp.applicationInfo.loadIcon(context
						.getPackageManager());
				// 得到應用最後一次更新的時間
				long lastUpdateTime = temp.lastUpdateTime;
				// 得到應用的包名
				String packageName = temp.packageName;
				// 得到應用的版本信息
				String versionName = temp.versionName;
				map = new HashMap();
				map.put(appName, appName);
				map.put(appIcon, appIcon);
				map.put(lastUpdateTime, lastUpdateTime);
				map.put(packageName, packageName);
				map.put(versionName, versionName);
				appInfos.add(map);// 把應用信息加入集合中返回
				map = null;
			} else {
				// 系統應用
			}
		}
		return appInfos;
	}

	// 獲取sd卡容量
	public static String getSdSize(Context context) {
		String totalStr = null, availStr = null;
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {// 判斷是否檢測到sd卡
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());

			long blockSize = stat.getBlockSizeLong();
			long availableBlocks = stat.getAvailableBlocksLong();
			long totalBlocks = stat.getBlockCountLong();

			long totalSize = blockSize * totalBlocks;
			long availSize = blockSize * availableBlocks;

			totalStr = Formatter.formatFileSize(context, totalSize);
			availStr = Formatter.formatFileSize(context, availSize);

		} else
			Toast.makeText(context, 沒有檢測到SD卡,請檢查是否正確插入, Toast.LENGTH_SHORT)
					.show();
		return SD卡總容量為: + totalStr + 可用為: + availStr;
	}

	// 獲取手機內存
	public static String getRomSave(Context context) {
		String totalStr = null, availStr = null;
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());// 得到手機內存的路徑

		long blockSize = stat.getBlockSizeLong();
		long availableBlocks = stat.getAvailableBlocksLong();
		long totalBlocks = stat.getBlockCountLong();

		long totalSize = blockSize * totalBlocks;
		long availSize = blockSize * availableBlocks;

		totalStr = Formatter.formatFileSize(context, totalSize);
		availStr = Formatter.formatFileSize(context, availSize);
		
		return 手機內存總大小為: + totalStr + 可用空間為: + availStr;
	}
}
三、網絡相關工具類
public class NetworkUtils {
	// 判斷當前網絡是否可用
	public static boolean networkIsConnect(Context context) {
		ConnectivityManager conn = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conn.getActiveNetworkInfo();
		if (info != null && info.isConnected()) {
			return true;
		} else {
			return false;
		}
	}

	// 判斷當前連接的網絡是否是wifi,並得到連接當前Wifi的信息
	public static boolean networkIsWifi(Context context) {
		ConnectivityManager conn = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conn.getActiveNetworkInfo();
		if (info != null && info.isConnected()
				&& info.getType() == ConnectivityManager.TYPE_WIFI) {
			WifiManager wifiManager = (WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			Toast.makeText(context, 連接的wifi網絡的id為:+wifiInfo.getNetworkId(), Toast.LENGTH_SHORT).show();
			return true;
		} else {
			return false;
		}
	}

	// 是否打開Wifi
	public static void setWifiEnabled(Context context, boolean enabled) {
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		if (enabled) {
			wifiManager.setWifiEnabled(true);
		} else {
			wifiManager.setWifiEnabled(false);
		}
	}

}
四、Px與Dp轉化工具類

 

 

public class DensityUtils {
	public static int Dp2Px(Context context, float dp) { 
	    final float scale = context.getResources().getDisplayMetrics().density; 
	    return (int) (dp * scale + 0.5f); 
	} 
	 
	public static int Px2Dp(Context context, float px) { 
	    final float scale = context.getResources().getDisplayMetrics().density; 
	    return (int) (px / scale + 0.5f); 
	} 
}
五、震動工具類

 

 

public class VibratorUtil {  
    
    /** 
     * long milliseconds :震動的時長,單位是毫秒 
     * long[] pattern  :自定義震動模式 。數組中數字的含義依次是[靜止時長,震動時長,靜止時長,震動時長。。。]時長的單位是毫秒 
     * boolean isRepeat : 是否反復震動,如果是true,反復震動,如果是false,只震動一次 
     */  
	 public static void Vibrate(final Context context) { 
		 long milliseconds = 100;
         Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);   
         vib.vibrate(milliseconds);   
  } 
     public static void Vibrate(final Context context, long milliseconds) {   
            Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);   
            vib.vibrate(milliseconds);   
     }   
     public static void Vibrate(final Context context, long[] pattern,boolean isRepeat) {   
            Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);   
            vib.vibrate(pattern, isRepeat ? 1 : -1);//-1為反復震動   
     }   
  
}  
 


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved