Android中的Notification

Notification

  Notification,即通知。这个估计是我们作为用户最不喜欢,作为开发者有不得不做的东西了。有时候我们手机一联网,通知栏里就会收到N多的推送消息,微博的新鲜事,淘宝什么什么又搞活动了……这些在我们通知栏里的都是Notification通知。那么他们是如何实现的呢?

NotificationManager创建Notification

通过NotificationManager来创建Notification。我们来看一下下面这个例子:
//定义了一个全局变量哦,方便使用
private NotificationManager mNotificationManager;
private void showNotification() {
        mNotification = new Notification();
        //设置通知栏的图片
        mNotification.icon = R.mipmap.ic_launcher;
        //设置状态栏上显示的消息
        mNotification.tickerText = "我是状态栏上的通知";
        //通过系统获得时间,通知的时间显示
        mNotification.when = System.currentTimeMillis() ;
        //设置通知可自动取消
        mNotification.flags=Notification.FLAG_AUTO_CANCEL;

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1, intent, PendingIntent.FLAG_ONE_SHOT);
        //设置通知的标题,内容以及点击通知启动的事件。
        mNotification.setLatestEventInfo(getApplicationContext(),"通知的标题", "我是通知的内容,显示在通知下拉栏中",pendingIntent);

        //设置启动id和启动的通知
        mNotificationManager.notify(1, mNotification);
    }

这里写图片描述

Notification.Builder创建Notification

通过Notification.Builder来创建Notification。我们来看一下下面这个例子:
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void showNotification2() {
        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
        Notification notification = new Notification.Builder(MainActivity.this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("我是显示在状态栏上的信息")
                .setContentTitle("我通知的标题")
                .setContentText("我是通知的内容")
                .setContentInfo("我是显示在时间下的内容")
                .setContentIntent(pendingIntent)
                .setAutoCancel(true).setWhen(System.currentTimeMillis()).build();
        mNotificationManager.notify(2, notification);
    }

这里写图片描述

自定义Notification

  自定义Notification虽然可以给我们更多的空间去发挥我们的想象力,去创造我们自己想要的Notification形式,但是在实际的开发中,Notification的形式简单单一,很少会使用自定义的Notification,即使用的少我们也应该掌握一下,下面看例子:

  自定义Notification:通过定义一个RemoteView后将其加载。

  RemoteView :TextView ,ImageView,ProgressBar属于RemoteView,Linearlayout也属于RemoteView等。
  
步骤:

  • 创建一个RemoteView的布局文件(xml), 该文件中只能包含TextView ,ImageView,ProgressBar控件和Linearlayout布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/imageview_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textview_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="这是通知的内容" />

    <ImageView
        android:id="@+id/imageview_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

</LinearLayout>

这里写图片描述

  • 创建 一个RemoteView的对象,将自定义的RemoteView加载在RemoteView的对象中。
    RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notification_remoteview);

  • 创建Notification的对象,设置其图片,内容等信息(虽然设置的信息是没有用的,但是如果不设置的话会进行报错,这是Android的怪癖!)

    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
        Notification notification = new Notification.Builder(MainActivity.this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker("我是显示在状态栏上的信息")
            .setContentTitle("我通知的标题")
            .setContentText("我是通知的内容")
            .setContentInfo("我是显示在时间下的内容")
            .setContent(remoteView)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true).setWhen(System.currentTimeMillis()).build();
       mNotificationManager.notify(2, notification);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小_爽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值