Notification——形形色色的通知(一)

  Notification在Android的App的使用中,非常常用。通常有两种情况,一种是需要给用户发一个通知,告知用户,然后用户点击可以打开详情页面;另一种情况是有时候会将一些耗时操作放在后台,不影响当前界面,但又希望用户知道后台的进度或者其他一些信息。在此,记录Notification的使用方法。

1.Notificiton需要一个视图,先用xml布局:

notify_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notify_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/notify_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/notify_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/notify_icon"
        android:indeterminate="false"/>
    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textSize="14sp"
        android:text="取消"
        />
    <LinearLayout
        android:id="@+id/text_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/notify_icon"
        android:layout_alignTop="@id/notify_icon"
        android:layout_toRightOf="@id/notify_icon"
        android:layout_toLeftOf="@id/cancel_btn"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="10dp">
        <TextView
            android:id="@+id/notify_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/notify_icon"
            android:paddingBottom="5dp"
            android:text="这是一条消息" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/notify_icon"
            android:layout_toRightOf="@id/notify_icon"
            android:text="点击查看消息" />
    </LinearLayout>
</RelativeLayout>

2.实现点击通知后的跳转界面

xml布局:

notify_des_layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notify_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/notify_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/notify_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/notify_icon"
        android:indeterminate="false"/>
    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textSize="14sp"
        android:text="取消"
        />
    <LinearLayout
        android:id="@+id/text_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/notify_icon"
        android:layout_alignTop="@id/notify_icon"
        android:layout_toRightOf="@id/notify_icon"
        android:layout_toLeftOf="@id/cancel_btn"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="10dp">
        <TextView
            android:id="@+id/notify_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/notify_icon"
            android:paddingBottom="5dp"
            android:text="这是一条消息" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/notify_icon"
            android:layout_toRightOf="@id/notify_icon"
            android:text="点击查看消息" />
    </LinearLayout>
</RelativeLayout>

3.初始化Notification

final static int UPLOAD_IMAGE = 111;
final static int NOTIFY_DES = 112;
private void initNotification(){
        String notifyTitle = "这是消息的标题";
        String notifyContent = "这是消息的内容";
        // 建立点击Intent
        Intent intent = new Intent(this , NotifyDesActivity.class);
        intent.putExtra("title" , notifyTitle);
        intent.putExtra("content", notifyContent);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFY_DES, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // 将notification的布局引入
        remoteViews = new RemoteViews(getPackageName() , R.layout.notify_layout);
        // 创建notificationpu 实例
        notifyBuilder = new NotificationCompat.Builder(this)
                // 通知时的图标
                .setSmallIcon(R.drawable.add_group_btn_img)
                // 下拉窗中的通知布局
                .setContent(remoteViews)
                // 通知时的提示
                .setTicker("收到一条消息")
                // 通知被点击时的事件
                .setContentIntent(pendingIntent)
                // 通知被点击后是否自动关闭
                .setAutoCancel(true);
    }    

PS:setSmallIcon是通知的时的图标,在通知结束后,这个图标会留在手机顶部。这个图标的尺寸是有限制的。

4.发出通知

    private void sendNotification(){
        // 获取默认通知铃声,此处可以自己设置
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        // 为通知设置铃声
        notifyBuilder.setSound(soundUri);
        // 为通知设置振动 停1000ms 震200ms 停1000ms 震200ms
        notifyBuilder.setVibrate(new long[]{1000, 200, 1000, 200});
        // 设置通知id,并发出通知
        notificationManager.notify(UPLOAD_IMAGE, notifyBuilder.build());
        notifyBuilder.setSound(null);
        notifyBuilder.setVibrate(null);
    }

PS:通知最重要的就是id,我可以再new一个通知实例,只要id相同,再发通知时,只会更新当前通知的信息。

其中

        notifyBuilder.setSound(null);
        notifyBuilder.setVibrate(null);

就是为了再次发起通知进行更新时,不会有振动和铃声的提示。

5.当我们想更新通知栏里的信息时,只需要设置相相关信息,然后以相同id重新发送通知即可。

    private void updateNotification() {
        remoteViews.setProgressBar(R.id.notify_progress , 100 , 50 ,false);
        notificationManager.notify();
    }

Done!

原文地址:https://www.cnblogs.com/fishbone-lsy/p/4713365.html