Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

android - How to detect expanding of status bar?

My application allows launching of other application from mine. None of my activity shows Status Bar.But when launching other applications like Camera the user can access the status bar.So i tried the following code snippet for collapsing the Status Bar inside a service(So it collapse every time and code running always).

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method collapse = null;
if(currentapiVersion <= 16){
    collapse = statusbarManager.getMethod("collapse");
}else{
    collapse = statusbarManager.getMethod("collapsePanels");
}
collapse.setAccessible(true);
collapse.invoke(service);

Now i want to collapse status bar only if user try to expand this.Is there any intent or intent filter exist for detect expanding of Status bar?

Thanks in Advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your activity override the onWindowFocusChanged() method and write the below code.

This uses the following permission:

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    try
    {
        if(!hasFocus)
        {
            Object service  = getSystemService("statusbar");
            Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
            Method collapse = statusbarManager.getMethod("collapse");
            collapse .setAccessible(true);
            collapse .invoke(service);
        }
    }
    catch(Exception ex)
    {
        if(!hasFocus)
        {
            try {
                Object service  = getSystemService("statusbar");
                Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
                Method collapse = statusbarManager.getMethod("collapse");
                collapse .setAccessible(true);
                collapse .invoke(service);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();                
            }
            ex.printStackTrace();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...