簡體   English   中英

有沒有辦法確定Android應用程序是否正在全屏運行?

[英]Is there a way to determine if Android app is running full screen?

我只是想知道是否有一種簡單的方法可以查看在 android 上運行的應用程序當前是否處於全屏模式。 有沒有辦法查詢 android 以查看您當前是否處於全屏模式或類似模式?

只是為了完成 Sigwann 的回答:

boolean fullScreen = (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;
boolean forceNotFullScreen = (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) != 0;
boolean actionbarVisible = getActionBar().isShowing();

我猜 Commonsware 想說 getWindow().getAttributes() 而不是 getWindow().getFlags(); 因為據我所知 getFlags 不存在。 至少它不在文檔中。

您需要讀取 getWindow().getAttributes().flags,這是一個 int。

WindowManager.LayoutParams lp = getWindow().getAttributes();
int i = lp.flags;

根據 android 文檔,FLAG_FULLSCREEN = 1024。 所以你的旗幟是 lp.flags 的第 11 口

如果此位 = 1,則激活全屏。

按照 Sigwann 的信息,這里是我的代碼....

public boolean isFullScreen() {
    int flg = getWindow().getAttributes().flags;
    boolean flag = false;       
    if ((flg & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
        flag = true;
    }
    return flag;
}

簡單,但有效!

您可以通過getWindow().getFlags()確定您的Activity是否正在全屏運行。

但是,如果“應用程序”指的是其他人的應用程序,那么答案是否定的。

從 api11 開始,現在有一種方法可以使用View.setOnSystemUiVisibilityChangeListener來檢測

偵聽器接口文檔注意以下幾點:

當狀態欄改變可見性時調用回調的接口定義。 這會報告系統 UI 狀態的全局更改,而不是應用程序請求的內容。

我不知道在 Honeycomb 之前是否有辦法做到這一點。

有一個很好的技巧,使用屏幕上View的絕對位置。 只需檢查布局的任何左上角視圖的位置(可以是不可見的),如果它在位置 0,0 上。 像這樣:

/**
 * Check if fullscreen is activated by a position of a top left View
 * @param topLeftView View which position will be compared with 0,0
 * @return
 */
public static boolean isFullscreen(View topLeftView) {
    int location[] = new int[2];
    topLeftView.getLocationOnScreen(location);
    return location[0] == 0 && location[1] == 0;
}

你可以使用這樣的東西;

findViewById(android.R.id.content).getHeight();
    getResources().getDisplayMetrics().heightPixels;

並檢查是否相等; 希望這會有所幫助;

有沒有辦法查詢 android 以查看您當前是否處於全屏模式或類似模式?

如果您的應用程序全屏運行,您可以獲得:

boolean isFullScreen = (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;

我只是想知道是否有一種簡單的方法可以查看應用程序是否在 android 上運行

如果您想了解應用程序是否在全屏模式下運行,而這僅以編程方式無法實現。

如果您想響應包括您的應用在內的所有應用的UI 可見性更改,請使用此代碼

View decorView = getWindow().getDecorView();//you can also use findViewById to get the view
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
}); 

信息來源

Activity.hasWindowFocus()告訴您您自己的活動是否對用戶可見。 但是,正如 CommonsWare 所指出的,要判斷另一個活動是否在堆棧的頂部要困難得多。 您可以嘗試查詢ActivityManager (例如ActivityManager.getProcessTasks() ),但它的方法不能為您的問題提供准確的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM