Android 一些功能代碼

mmp7 9年前發布 | 895 次閱讀 Java

全屏

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);
}
 
 本文由用戶 mmp7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!