0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Android] StatusBarManagerを使ったNotificationパネルのオンオフ方法

Last updated at Posted at 2019-10-19

AndroidアプリでStatusBarManagerという非公開(API level 29から公式化?)のAPIを利用して、Notificationパネル(プッシュ通知一覧のやつ)の表示オンオフする方法を説明します。

StatusBarManagerソースコード

StatusBarManagerのソースコードはここにあります。

必要なパーミッション

以下をAndroidManifest.xmlに追加する必要があります。runtimeパーミッションではないので、AndroidManifest.xmlに追加するだけでOKです。

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

ソースコード

非公開扱いのクラスのため、何が起きてもいいようにtry/catchは入れておきましょう。パネルを開く場合は、android.app.StatusBarManager.expandNotificationsPanelをコールします。

パネルを開くサンプル
if(ContextCompat.checkSelfPermission(this, Manifest.permission.EXPAND_STATUS_BAR) != PackageManager.PERMISSION_GRANTED)
    return;

try {
    Object service = getSystemService("statusbar");
    Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
    Method expand = statusBarManager.getMethod("expandNotificationsPanel");
    expand.invoke(service);
}
catch (Exception e){
    e.printStackTrace();
}

逆にパネルを閉じる場合は、クイック設定画面も同時に閉じる事になりますが、android.app.StatusBarManager.collapsePanelsをコールします。

パネルを閉じるサンプル
if(ContextCompat.checkSelfPermission(this, Manifest.permission.EXPAND_STATUS_BAR) != PackageManager.PERMISSION_GRANTED)
    return;

try {
    Object service = getSystemService("statusbar");
    Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
    Method expand = statusBarManager.getMethod("collapsePanels");
    expand.invoke(service);
}
catch (Exception e){
    e.printStackTrace();
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?