Android 中

通知

2019-04-17  本文已影响1人  穿越平行宇宙

Android中Notification 提示对话框,notification

notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。

一、notification用于状态栏显示通知的控件,在不同的设备上面notification是不一样的

二、notification的基本布局

元素组成:

icon/photo:大图标 tiltle/name:标题 message:内容消息 timestamp:通知的时间,默认是系统发出的时间,也可以通过setwhen()来设置 secondary icon小图标

三、notification的使用基本使用流程

状态通知栏主要涉及到了两个类:notification和notificationmanager

notification:通知信息类,他里面对应了通知栏的各个属性

notificationmanager:是状态栏通知的管理类,负责发通知、清楚通知等操作

此外我们还可以调用notificationmanager的cancel()方法取消通知

四、设置相关的一些方法

notification.builder mbuilder = new notification.builder(this); 

后再调用下述的相关的方法进行设置,常用的方法如下:
[]):设置振动方式,比如:

.setdefaults(notification.default_sound) //获取默认铃声
.setsound(uri.parse("file:///sdcard/xx/xx.mp3")) //获取自定义铃声
.setsound(uri.withappendedpath(audio.media.internal_content_uri, "5")) //获取android多媒体库内的铃声

flag_one_shot表示返回的pendingintent仅能执行一次,执行完后自动取消
flag_no_create表示如果描述的pendingintent不存在,并不创建相应的pendingintent,而是返回null
flag_cancel_current 表示相应的pendingintent已经存在,则取消前者,然后创建新的pendingintent, 这个有利于数据保持为最新的,可以用于即时通信的通信场景
flag_update_current 表示更新的pendingintent

使用示例:

//点击后跳转activity
intent intent = new intent(context,xxx.class); 
pendingintent pendingintent = pendingintent.getactivity(context, 0, intent, 0); 
mbuilder.setcontentintent(pendingintent)

|

setpriority(int):设置优先级:

优先级

用户

max

重要而紧急的通知,通知用户这个事件是时间上紧迫的或者需要立即处理的。

high

高优先级用于重要的通信内容,例如短消息或者聊天,这些都是对用户来说比较有兴趣的。

default

默认优先级用于没有特殊优先级分类的通知。

low

低优先级可以通知用户但又不是很紧急的事件。

min

用于后台消息 (例如天气或者位置信息)。最低优先级通知将只在状态栏显示图标,只有用户下拉通知抽屉才能看到内容。

对应属性:notification.priority_high..

五、基本使用实例

xml文件:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送通知" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消通知" />

</LinearLayout>

java文件:
MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 发送通知
     */
    private Button mBtn1;
    /**
     * 取消通知
     */
    private Button mBtn2;
    private NotificationManager manager;
    private Notification mNotification;
    private static final int notifyid_1 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mBtn1 = (Button) findViewById(R.id.btn1);
        mBtn1.setOnClickListener(this);
        mBtn2 = (Button) findViewById(R.id.btn2);
        mBtn2.setOnClickListener(this);

        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn1:

                // 定义一个pendingintent,点击intent可以启动一个新的intent
                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                PendingIntent pit = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

                // 设置图片文字提示方式等等
                Notification.Builder builder = new Notification.Builder(MainActivity.this);
                builder.setContentTitle("叶良辰") //标题
                        .setContentText("我有一百种方法让你呆不下去~") //内容
                        .setSubText("――记住我叫叶良辰") //内容下面的一小段文字
                        .setTicker("收到叶良辰发送过来的信息~") //收到信息后状态栏显示的文字信息
                        .setWhen(System.currentTimeMillis()) //设置通知时间
                        .setSmallIcon(R.mipmap.ic_launcher) //设置小图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE) //设置默认的三色灯与振动器
                        .setAutoCancel(true) //设置点击后取消notification
                        .setContentIntent(pit);

                mNotification = builder.build();
                manager.notify(notifyid_1, mNotification);

                break;
            case R.id.btn2:

                manager.cancel(notifyid_1);

                break;
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读