通知栏消息(Notification)初步

  

Notification是用来在通知中心中显示信息的,这里讲解了其最简单的使用方式。

关于PendingIntent和Intent的区别可以参考这篇文章:http://blog.csdn.net/zeng622peng/article/details/6180190

MainActivity.java

package com.kale.notification;

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 {

    static final int NOTIFICATION_ID = 0x123;
    NotificationManager nm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取系统服务来初始化对象
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        initView();

    }

    private void initView() {
        // TODO 自动生成的方法存根
        Button b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO 自动生成的方法存根
                sent(arg0);
            }
        });
        Button b2 = (Button)findViewById(R.id.button2);
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO 自动生成的方法存根
                del(arg0);
            }
        });
    }

    public void sent(View source) {
        //设置点击后启动的activity
        Intent intent = new Intent(MainActivity.this,MainActivity.class);
        //将Intent封装进PendingIntent中,点击通知的消息后,就会启动对应的程序
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Notification notification = new Notification.Builder(this)
        .setAutoCancel(true)//点击通知后,自动消失
        .setTicker("在顶部状态栏的提示信息")//在顶部状态栏中的提示信息
        .setSmallIcon(R.drawable.ic_launcher)//设置顶部状态栏的小图标
        .setContentTitle("这是内容的标题")//设置通知中心的标题
        .setContentText("这是通知的内容")//设置通知中心中的内容
        .setDefaults(Notification.DEFAULT_SOUND|//设置使用默认的声音
                Notification.DEFAULT_LIGHTS|//设置使用默认的LED
                Notification.DEFAULT_VIBRATE)//设置使用默认的振动
        //.setVibrate(new Long[] {0,50,100,150}) 设置自定义的振动
        /*.setSound(Uri.parse("file:///sdcard/click.mp3"))*/
        .setWhen(System.currentTimeMillis())
        .setContentIntent(pIntent)//该通知要启动的Intent
        .build();
        //发送该通知
        nm.notify(NOTIFICATION_ID,notification);
    }

    public void del(View v) {
        //取消通知
        nm.cancel(NOTIFICATION_ID);
    }

}

补充内容(来自:http://www.oschina.net/code/snippet_270292_14489):

NotificationManager常用方法介绍:

public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)

public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)

public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id

public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id

常量:

DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等

DEFAULT_LIGHTS 使用默认闪光提示

DEFAULT_SOUNDS 使用默认提示声音

DEFAULT_VIBRATE 使用默认手机震动

 

以上的效果常量可以叠加,即通过

notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE; 

notification.defaults |= DEFAULT_SOUND 

<!-- 闪光灯的权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 振动的权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>

布局文件(仅仅就是两个按钮 ⊙﹏⊙b汗)

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kale.notification.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="18dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="发送" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="47dp"
        android:text="取消" />

</RelativeLayout>

通知栏消息(Notification)初步

时间: 2024-07-28 15:02:40

通知栏消息(Notification)初步的相关文章

Android 之 信息通知栏消息Notification

Notification是安卓手机顶部的消息提示 这里我们分别设置两个按钮,来实现顶部消息的发送和取消 功能实现 首先要在主Activity中设置一个通知控制类 NotificationManager manager; //通知控制类 然后在onCreate方法中获取系统服务 manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 接着设置通知栏信息 private void sendNoti

通知栏发送消息Notification(可以使用自定义的布局)

一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见.代码在此时机发送一个Notification到通知栏.当用户点击通知栏的Notification后,又重新onRestart() -> onStart() -> onResume() 切换回原Activity. 1 package com.zzw.testnotification; 2 3 import android.app.Ac

android实现通知栏消息

一.原理 消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来:另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息. 二.步骤(代码) 注: Notification //是具体状态栏对象,设置Icon.文字.声音等.NotificationMangager //状态栏通知管理类.负责发消息.清理消息. import android.app.Notification;import android.app.Notification

Android手机——读取手机电话+短信+网页+图片+音乐+视频+APK+通知栏消息+换头像

Android手机--电话+短信+网页+图片+音乐+视频+APK+通知栏消息+换头像 <!--拨打电话权限--> <uses-permission android:name="android.permission.CALL_PHONE"/> <!--连接网络权限--> <uses-permission android:name="android.permission.INTERNET"/> <!--读写文件的权限

Android中使用Notification实现宽通知栏(Notification示例二)

Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看.通知栏和通知抽屉 (notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app. Notification 的设计: 作为android UI中很重要的组成部分,notification拥有专属于自己的设计准则. Notification的界面元素在通知抽屉中的

Android中使用Notification实现普通通知栏(Notification示例一)

Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看.(notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app. Notification的设计: 作为android UI中很重要的组成部分,notification拥有专属于自己的设计准则. Notification的界面元素在通知抽屉中的notificati

Xamarin 小试牛刀 通知栏消息通知和按钮(基于Java代码人肉转换)

本示例基于网友现有安卓项目人肉翻译,在Xamarin中替换和修改了很多方法的命名,比如某些属性需要去掉getName的get前缀, 有些方法名称需要使用Pascal命名法替换Java的Camel 命名规范 另外在内部类的使用方式上也有一些区别,但是整体上来说,大部分的方法名称都与Java 原版Android一致,所以如果有现有的Android 项目需要转换到Xamarin 还是很容易的.此处给Xamarin 66个赞 参考Java版本:http://blog.csdn.net/wxdjaqgs/

android-带进度条的系统通知栏消息

效果图: 主界面只有一个按钮就不上文件了 通知栏显示所用到的布局文件content_view.xml [java] view plaincopy <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fi

Java消息服务初步学习(基于Spring In Action的整理)

几个名词 Java消息服务(Java Message Service)是一个Java标准,定义了使用消息代理的通用API. 消息代理(message broker):类似于邮局的作用,确保消息被投递到指定的目的地. ActiveMQ Kafka 目的地(destination) 队列(queue,点对点模型):消息可以有多个接收者,但每一条消息只能被一个接收者取走. 主题(topic,点对线模型):订阅此主题的订阅者都会接收到此消息的副本. 异步消息相较于同步消息的优点 时间:异步消息不需要等待