Android组件11—Notification

这篇博客介绍了Android中Notification的使用,旨在帮助初学者复习和理解。Notification是展示在手机状态栏的消息,通常由NotificationManager服务发送。博客内容包括效果展示、代码实现和XML布局解析,提供了简单的代码示例和跳转页面的创建方法。
  • 今天我朋友问我说,你写这些太简单了,没人会看。但是我想说的是,我这么写只是想重新复习下,能帮助一下其他小白我很开心,在我写博客的时候出发点就很明确,并不是为了写博客而写,而是把博客当作自己笔记,自己需要的时候能很快东找到。所以我不会去无缘无故的乱辅助那些大牛的东西,没必要。除非我研究透自己写出来例子!

Notification的介绍和用法

Notification是显示在手机状态栏的消息—-手机状态栏位于手机屏幕的最上方,哪里一般显示了手机当前的网络状态、电池状态。Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。

先看一下效果

这里写图片描述

那上面效果是如何实现,其实很简单,看下面代码

  • 1、看一下代码
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity
{
    static final int NOTIFICATION_ID = 0x123;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取系统的NotificationManager服务
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }
    // 为发送通知的按钮的点击事件定义事件处理方法
    public void send(View source)
    {
        // 创建一个启动其他Activity的Intent
        Intent intent = new Intent(MainActivity.this, OtherActivity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        Notification.Builder notify = new Notification.Builder(this);
            // 设置打开该通知,该通知自动消失
        notify.setAutoCancel(true);
            // 设置显示在状态栏的通知提示信息
        notify.setTicker("有新消息");
            // 设置通知的图标;;
        notify.setSmallIcon(R.drawable.notify);
            // 设置通知内容的标题
        notify.setContentTitle("一条新通知");
            // 设置通知内容
        notify.setContentText("恭喜你,您加薪了,工资增加20%!");
            // 设置使用系统默认的声音、默认LED灯
            // .setDefaults(Notification.DEFAULT_SOUND
            // |Notification.DEFAULT_LIGHTS)
            // 设置通知的自定义声音
        notify.setSound(Uri.parse("android.resource://org.crazyit.ui/"+ R.raw.msg));
        notify.setWhen(System.currentTimeMillis());
            // 设改通知将要启动程序的Intent
        notify.setContentIntent(pi);  // ①

        // 发送通知
        nm.notify(NOTIFICATION_ID, notify.build());
    }
    // 为删除通知的按钮的点击事件定义事件处理方法
    public void del(View v)
    {
        // 取消通知
        nm.cancel(NOTIFICATION_ID);
    }
}
  • 1、上面代码很简单,现要获取系统的服务

    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

  • 2、创建跳转页面

        Intent intent = new Intent(MainActivity.this, OtherActivity.class);

        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);


  • 3、最后就是创建一个notification对象

Notification.Builder notify = new Notification.Builder(this);
然后就是用notify.xxx()*方法来引用你自己需要的方法。例如:
这里写图片描述

  • 代码中我用了其中的几个特别熟悉的方法,其他有需要的大家自己去百度。

再看一下xml布局

activity布局

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送Notification"
        android:onClick="send"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除Notification"
        android:onClick="del"
        />
</LinearLayout>

other.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">
<!-- 定义一个ImageView -->
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/call_erduo"
    android:layout_gravity="center_horizontal"
    />

    <Button
        android:id="@+id/other"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回"/>
</LinearLayout>

dome:http://download.csdn.net/detail/bobo8945510/9643094

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值