notification组件(一)——系统原生

notification就是在android信息栏中的一个用于与用户交互的组件(例如:当短信来的时候,会在信息栏里显示一个notification组件)

实现android原生的notification需要以下几个步骤即可实现

step1:调用getSystemService()方法获取系统的notificationmanager服务

step2:创建一个notification对象,并未其设置各种属性

step3:为notification对象设置事件信息

step4:通过notificationmanager类的notify()方法发送notification通知

实现android原生的notification只需要以上4个简单步骤

下面通过一个实例讲解:

首先先定义一个linearlayout布局文件,添加两个按钮button,一个是显示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"
    tools:context="${packageName}.${activityClass}"
    android:orientation="vertical" >
    <!-- 添加两个button按钮,一个是显示notification按钮,一个是清除notification按钮 -->
    <Button
        android:id="@+id/Bxianshi_notification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Bxianshi_notification"/>
    <Button
        android:id="@+id/Bqingchu_notification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Bqingchu_notification"/>
    <Button
        android:id="@+id/Bqingchu_notification_flag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Bqingchu_notification_flag"/>
</LinearLayout>



在java代码中实现布局中的三个按钮产生的效果

package com.testnotification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /**
     * 定义两个notifiacation的标识
     */
    final int NOTIFICATION_1 = 1;
    final int NOTIFICATION_2 = 2;
    /**
     * 定义两个button的对象
     */
    Button bn1,bn2,bn3;
    /**
     * 定义ticker的变量值
     */
    String tickertext = "来信息了";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 获取notificationmanager的对象
         */
        final NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        /**
         * 获取button按钮
         * 设置两个按钮对应的事件信息
         */
        bn1 = (Button) findViewById(R.id.Bxianshi_notification);
        bn2 = (Button) findViewById(R.id.Bqingchu_notification);
        bn3 = (Button) findViewById(R.id.Bqingchu_notification_flag);
        bn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                /**
                 * 此处设置实在为拉开信息栏时显示的信息
                 * 定义一个notification的对象
                 * 设置icon
                 * 设置未拉开信息栏时提示的文字信息
                 * 设置当前时间发送
                 * 设置默认声控光电效应
                 */
                Notification notification_show = new Notification();
                notification_show.icon = R.drawable.ic_launcher;
                notification_show.tickerText = tickertext;
                notification_show.when = System.currentTimeMillis();
                notification_show.defaults = Notification.DEFAULT_ALL;
                /**
                 * 设置拉开信息栏后的显示信息
                 * notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                 * 上面的参数中第一个是当前你要指定在那个环境下显示,第二个是标题,第三个是正文,第四个是事件
                 */
                notification_show.setLatestEventInfo(MainActivity.this, "test", "hello suntai!", null);
                /**
                 * 把notification标识和notification的实例对应起来发出来
                 */
                notificationmanager.notify(NOTIFICATION_1, notification_show);
            }
        });
        bn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //这个方法将未拉下信息栏时定义一个notification的对象并初始化属性
                Notification notification_cancel = new Notification(R.drawable.ic_launcher, "second notification", System.currentTimeMillis());
                //定义当点击这个notification后清除这个notification
                notification_cancel.flags |= Notification.FLAG_AUTO_CANCEL;
                //定义notification的意图和延迟意图
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                PendingIntent pendingintent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                //定义notification在信息栏显示的信息
                notification_cancel.setLatestEventInfo(MainActivity.this, "second notification", "hoho", pendingintent);
                //将标识和notification关联起来发送
                notificationmanager.notify(NOTIFICATION_2, notification_cancel);
            }
        });
        bn3.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //这个方法是定义按照notification的标识清除
                //notificationmanager.cancel(NOTIFICATION_1);
                //这个方法是清除全部的notification(只限于当前关联的notification)
                notificationmanager.cancelAll();
            }
        });
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值