需要权限:
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
收起通知栏(经测能用)
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();
}
}
转自文章:折叠与展开通知栏方法
网友评论