android notification 的总结分析
android notification 的总结分析
摆渡者 发表于3年前
android notification 的总结分析
  • 发表于 3年前
  • 阅读 304
  • 收藏 2
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

摘要: notification是一种出现在任务栏的提示,特别是在4.0以后notification改进了不少,本文内容都是基于4.0及4.1以后总结来的.

分类

 Android的notification有以下几种:

1>普通notification

图1

  1. 标题,通过NotificationCompat.Builder.setContentTitle(String title)来设置

  2. 大图标,通过NotificationCompat.Builder.setLargeIcon(Bitmap icon)来设置

  3. 内容,通过NotificationCompat.Builder.setContentText("ContentText")来设置

  4. 内容附加信息通过NotificationCompat.Builder.setContentInfo("ContentInfo")来设置

  5. 小图标,通过NotificationCompat.Builder.setSmallIcon(int icon)来设置

  6. 时间,通过NotificationCompat.Builder.setWhen(when)来设置

注:

一个notification不必对上面所有的选项都进行设置,但有3项是必须的:

  • 小图标, set by setSmallIcon()

  • 标题, set by setContentTitle()

  • 内容, set by setContentText()

2>大布局Notification

图2

大布局notification是在android4.1以后才增加的,大布局notification与小布局notification只在‘7'部分有区别,其它部分都一致。大布局notification只有在所有notification的最上面时才会显示大布局,其它情况下显示小布局。你也可以用手指将其扩展为大布局(前提是它是大布局)。如下图:

图3

大布局notification有三种类型:如图2为NotificationCompat.InboxStyle 类型。图3左部为NotificationCompat.BigTextStyle。图3右部 为:NotificationCompat.BigPictureStyle.

InboxStyle类型的notification看起来和BigTextStyle类型的notification,那么他们有什么不同呢?对于InboxStyle类型的notification,图2的‘7’位置处每行都是很简短的,第一行和最后两行由于内容很长,则使用了省略号略去了过长的内容;而图3的左图中,BigTextStyle类型的notification则是将过长的内容分在了多行显示

3>自定义布局notification

除了系统提供的notification,我们也可以自定义notification。如下图所示的一个音乐播放器控制notification:


图4

创建自定义的notification

    1>实例化一个NotificationCompat.Builder对象;如builder

    2>调用builder的相关方法对notification进行上面提到的各种设置

    3>调用builder.build()方法此方法返回一个notification对象。

    4>获取系统负责通知的NotificationManager;如:manager

    5>调用manager的notify方法。


示例代码

示例程序截图:

图5

0>初始化部分代码

public class MainActivity extends Activity implements OnClickListener {

    private int[] btns = new int[] { 
            R.id.normal, R.id.inboxStyle, 
            R.id.bigTextStyle, R.id.bigPicStyle, 
            R.id.customize, R.id.progress,
            R.id.cancelNotification };
    private NotificationManager manager;
    private Bitmap icon = null;

    private static final int NOTIFICATION_ID_NORMAL = 1;
    private static final int NOTIFICATION_ID_INBOX = 2;
    private static final int NOTIFICATION_ID_BIGTEXT = 3;
    private static final int NOTIFICATION_ID_BIGPIC = 4;
    private static final int NOTIFICATION_ID_CUSTOMIZE = 5;
    private static final int NOTIFICATION_ID_PROGRESS = 6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取系统的通知服务
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        for (int btn : btns) {
            findViewById(btn).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.normal:
            showNormalNotification();
            break;
        case R.id.inboxStyle:
            showInboxStyleNotification();
            break;
        case R.id.bigTextStyle:
            showBigTextStyleNotification();
            break;
        case R.id.bigPicStyle:
            showBigPicStyleNotification();
            break;
        case R.id.customize:
            showCustomizeNotification();
            break;
        case R.id.progress:
            showProgressBar();
            break;
        case R.id.cancelNotification:
            cancelNotification();
            break;
        default:
            break;
        }
    }
}

1>普通notification

private void showNormalNotification() {
    Notification notification = new NotificationCompat.Builder(this)
            .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
            .setTicker("NormalNotification").setContentInfo("ContentInfo")
            .setContentTitle("ContentTitle").setContentText("ContentText")
            .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
            .build();
    manager.notify(NOTIFICATION_ID_NORMAL, notification);
}

2>大布局Text类型notification

private void showBigTextStyleNotification() {
    NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle();
    textStyle.setBigContentTitle("BigContentTitle")
            .setSummaryText("SummaryText")
            .bigText("I am Big Texttttttttttttttttttttttttttttttttt"
                    + "tttttttttttttttttttttttttttttttttttttttttttt"
                    + "!!!!!!!!!!!!!!!!!!!......");
    Notification notification = new NotificationCompat.Builder(this)
            .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
            .setTicker("showBigTextStyleNotification").setContentInfo("contentInfo")
            .setContentTitle("ContentTitle").setContentText("ContentText")
            .setStyle(textStyle).setAutoCancel(false)
            .setShowWhen(false).setDefaults(Notification.DEFAULT_ALL)
            .build();
    manager.notify(NOTIFICATION_ID_BIGTEXT, notification);
}

3> 大布局Inbox类型notification

private void showInboxStyleNotification() {
    String[] lines = new String[]{"line1", "line2", "line3"};
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle("BigContentTitle")
        .setSummaryText("SummaryText");
    for (int i = 0; i < lines.length; i++) {
        inboxStyle.addLine(lines[i]);
    }
    Notification notification = new NotificationCompat.Builder(this)
            .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
            .setTicker("showBigView_Inbox").setContentInfo("ContentInfo")
            .setContentTitle("ContentTitle").setContentText("ContentText")
            .setStyle(inboxStyle).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .build();
    manager.notify(NOTIFICATION_ID_INBOX, notification);
}

4>大布局Picture类型notification

private void showBigPicStyleNotification() {
    NotificationCompat.BigPictureStyle pictureStyle = new NotificationCompat.BigPictureStyle();
    pictureStyle.setBigContentTitle("BigContentTitle")
            .setSummaryText("SummaryText")
            .bigPicture(icon);
    Notification notification = new NotificationCompat.Builder(this)
            .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
            .setTicker("showBigPicStyleNotification").setContentInfo("ContentInfo")
            .setContentTitle("ContentTitle").setContentText("ContentText")
            .setStyle(pictureStyle)
            .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
            .build();
    manager.notify(NOTIFICATION_ID_BIGPIC, notification);
}

5>自定义notification
效果图:

图6

并对中间的播放按钮做了一个简单的点击处理事件:

private void showCustomizeNotification() {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
    Intent intent = new Intent(this, PlayMusicActivity.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, pendingIntent);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContent(remoteViews).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(icon).setOngoing(true)
            .setTicker("music is playing").setDefaults(Notification.DEFAULT_ALL);
    manager.notify(NOTIFICATION_ID_CUSTOMIZE, builder.build());
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="center_vertical"
     android:orientation="horizontal" >

     <ImageView
         android:id="@+id/singer_pic"
         android:layout_width="64dp"
         android:layout_height="64dp"
         android:src="@drawable/singer" />

     <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:gravity="center_vertical"
         android:orientation="horizontal" >

         <ImageView
             android:id="@+id/last_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/player_previous" />

         <ImageView
             android:id="@+id/paly_pause_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/player_pause" />

         <ImageView
             android:id="@+id/next_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/player_next" />
     </LinearLayout>
 </LinearLayout>

带进度条的notification:

private void showProgressBar() {
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
            .setTicker("showProgressBar").setContentInfo("ContentInfo")
            .setOngoing(true).setContentTitle("Downloading...")
            .setContentText("ContentText");
    new Thread(new Runnable() {
        @Override
        public void run() {
            int progress = 0;
            for (progress = 0; progress < 100; progress += 5) {
                //将setProgress的第三个参数设为true即可显示为无明确进度的进度条样式
                //builder.setProgress(100, progress, true);
                builder.setProgress(100, progress, false);
                manager.notify(NOTIFICATION_ID_PROGRESS, builder.build());
                try {
                    // Sleep for 5 seconds
                    Thread.sleep(2 * 1000);
                } catch (InterruptedException e) {
                }
            }
            builder.setContentTitle("Download complete")
                    .setProgress(0, 0, false).setOngoing(false);
            manager.notify(NOTIFICATION_ID_PROGRESS, builder.build());
        }
    }).start();
}


标签: Android notification
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 307
博文 169
码字总数 205794
评论 (0)
×
摆渡者
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: