需要权限:

收起通知栏(经测能用)

public void collapseStatusBar() {

Object service =getSystemService("statusbar");

if (null == service)

return;

try {

Class> clazz = Class.forName("android.app.StatusBarManager");

int sdkVersion = android.os.Build.VERSION.SDK_INT;

Method collapse = null;

if (sdkVersion <= 16) {

collapse = clazz.getMethod("collapse");

} else {

collapse = clazz.getMethod("collapsePanels");

}

collapse.setAccessible(true);

collapse.invoke(service);

} catch (Exception e) {

e.printStackTrace();

}

}

展开通知栏(未测试)

/**

* 展开通知栏

* @param context

*/

public static void expandNotification(Context context) {

Object service = context.getSystemService("statusbar");

if (null == service)

return;

try {

Class> clazz = Class.forName("android.app.StatusBarManager");

int sdkVersion = android.os.Build.VERSION.SDK_INT;

Method expand = null;

if (sdkVersion <= 16) {

expand = clazz.getDeclaredMethod("expand");

} else {

/*

* Android SDK 16之后的版本展开通知栏有两个接口可以处理

* expandNotificationsPanel()

* expandSettingsPanel()

*/

//expand =clazz.getMethod("expandNotificationsPanel");

expand = clazz.getDeclaredMethod("expandSettingsPanel");

}

expand.setAccessible(true);

expand.invoke(service);

} catch (Exception e) {

// //e.printStackTrace();

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐