Android 一些功能代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

全屏
requestWindowFeature(Window.FEATURE_NO_TITLE); // 无标题栏需要在setContentView之前
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
也可以在manifest中:
<Activity android:theme="@android:style/Theme.NoTitleBar.Fullscreen".. />

横屏
>setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
安装apk

private void install(File file) {
	Intent intent = new Intent();
	intent.setAction(Intent.ACTION_VIEW);
//	intent.setData(Uri.fromFile(file));
//	intent.setType("application/vnd.android.package-archive"); //mime的数据类型 plain/text image/jpeg 
	intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");//该mime 表示 .apk 文件类型
	startActivity(intent);
}
精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕) 


public static double getScreenPhysicalSize(Activity ctx) {
	DisplayMetrics dm = new DisplayMetrics();
	ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
	//对角线像素长
	double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
	return diagonalPixels / (160 * dm.density); //160像素,density=1
}

启动Apk的默认Activity



public static void startApkActivity(final Context ctx, String packageName) {
	PackageManager pm = ctx.getPackageManager();
	PackageInfo pi;
	try {
	<span style="white-space:pre">	</span>pi = pm.getPackageInfo(packageName, 0);
		Intent intent = new Intent(Intent.ACTION_MAIN, null);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setPackage(pi.packageName);

		List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);

		ResolveInfo ri = apps.iterator().next();
		if (ri != null) {
			String className = ri.activityInfo.name;
			intent.setComponent(new ComponentName(packageName, className));
			ctx.startActivity(intent);
		}
	} catch (NameNotFoundException e) {
		Log.e("startActivity", e.getMessage());
	}
}


 根据手机的分辨率从 dp 的单位 转成为 px(像素) 

  

public static int dip2px(Context context, float dpValue) {  
	final float scale = context.getResources().getDisplayMetrics().density;  
	return (int) (dpValue * scale + 0.5f);  
}  


根据手机的分辨率从 px(像素) 的单位 转成为dp   


public static int px2dip(Context context, float pxValue) {  
<span style="white-space:pre">	</span>final float scale = context.getResources().getDisplayMetrics().density;  
	return (int) (pxValue / scale + 0.5f);  
}  
重启应用程序


Intent i = getBaseContext().getPackageManager()//
	.getLaunchIntentForPackage(getBaseContext().getPackageName()); 
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(i);

判断字宽


public static float GetTextWidth(String text, float size) {
	TextPaint fontPaint = new TextPaint();
	fontPaint.setTextSize(size);
	return fontPaint.measureText(text);
}