LoginSignup
14
13

More than 5 years have passed since last update.

ステータスバーの通知アイコンを非表示にする

Posted at

やりたいこと

常駐するサービスでForeground属性を使うため、Notificationを指定している。
ステータスバーへのアイコン表示は目立つため、できるなら消したい。

以前は空のNotificationインスタンスを渡すと、Foreground属性を指定していても通知領域には表示されなかった。

最近のAndroidだと、空のnotificationをForeground属性の指定に利用するとシステムが自動生成するnotificationが表示される。(Nexus5で確認)

(参考:iBattery)
status_bar_icon.png
notification_sample.png

この記事では、通知欄からNotificationを消すことはできないが、ステータスバーへの通知は見えないようにする方法を紹介します。
(ユーザが嫌がるのはステータスバーに常に表示されてることだと思う...)

やること

実現するためにやることは2つです。

  • 通知アイコンを透明にする
  • ステータスバー上の表示位置を右側によせる

通知アイコンを透明にする

これは簡単でNotification.Builder#setSmallIcon()に透明なdrawableを渡せばいいです。

res/drawable/ic_stat_transparent.xml

ic_stat_transparent.xml
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#00000000" >
</color>

このままだと通知欄を展開した時のアイコンも透明のままなので、ランチャーアイコンなどのBitmapをNotification.Builder#setLargeIcon()で指定します。

Sample
Drawable largeIconDrawable = getResources().getDrawable(R.drawable.ic_launcher);
Bitmap largeIconBitmap = ((BitmapDrawable)largeIconDrawable).getBitmap();

notificationBuilder.setLargeIcon(largeIconBitmap);

ステータスバー上の表示位置を右側によせる

ステータスバー上のアイコンの位置はAPI 16から用意されているNotificationのPriority指定である程度操作することができます。

Notification.Builder#setPriority(int pri)で設定できます。
用意されている定数は
PRIORITY_MAX = 2
PRIORITY_HIGH = 1
PRIORITY_DEFAULT = 0
PRIORITY_LOW = -1
PRIORITY_MIN = -2
の5つです。
数値が大きいほど左側に表示されるので、PRIORITY_MINを使います。

Sample
// API 16以上のみ指定する
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
    notificationBuilder.setPriority(Notification.PRIORITY_MIN);
}

これで透明な通知アイコンが一番右側に表示されるので、通知アイコンが見えないようになりました。

通知欄からも消せたらベストだけど、さすがにそれはできなさそう...

14
13
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
14
13