Android 使用Notification进行消息提示

一.概述

今天来讲讲Notification的使用,先看效果图。
这里写图片描述

二.代码

首先是布局文件,我们定义三个按钮

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.notificationdemo.MainActivity">
    <Button
        android:id="@+id/normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="发送普通通知"
        />
        <Button
            android:id="@+id/custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送自定义通知"
            android:onClick="click"
            />
    <Button
        android:id="@+id/dismiss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="取消通知"
        />
</LinearLayout>

接下来看主要代码,我会给出比较详细的注释:

public class MainActivity extends AppCompatActivity {
    private NotificationManager manager;
    private static final int NOTIFYID = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取通知服务
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }
    public void click(View view){
        switch (view.getId()){
            case R.id.normal:
                sendNormalNotification();
                break;
            case R.id.custom:
                sendCustomNotification();
                break;
            case R.id.dismiss:
//                manager.cancel(NOTIFYID);//根据通知id取消对应的通知
                manager.cancelAll();//取消所有的通知
                break;
        }
    }
    /**
     * 发送普通通知
     */
    public void sendNormalNotification(){
         Notification.Builder builder = new Notification.Builder(this);
        builder.setTicker("消息来了");//设置提示消息
        builder.setContentText("今天出去吃饭吧");//通知内容
        builder.setContentTitle("提示");//通知标题
        builder.setSmallIcon(R.mipmap.head);//提示消息旁边的小图标
        builder.setNumber(2);//设置右边显示的数字
        builder.setOngoing(true);//是否可以滑动移除通知,true代表可以
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));//设置大图标
        Notification notify = builder.build();
        //点击通知启动的页面
        Intent intent = new Intent(this,MessageActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        //启动通知
        manager.notify(NOTIFYID,notify);
    }
    /**
     * 自定义通知
     */
    public void sendCustomNotification(){
        Notification.Builder builder2 = new Notification.Builder(this);
        builder2.setTicker("开始下载");
        builder2.setSmallIcon(R.mipmap.ic_launcher);
        builder2.setWhen(System.currentTimeMillis());
        //第一个参数为包名。第二个参数为通知布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.cunstom_layout);
        builder2.setContent(remoteViews);
        Notification notify = builder2.build();
        manager.notify(1,notify);
    }
}

源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值