通知栏消息(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>
复制代码
posted @   developer_Kale  阅读(561)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
网站流量统计工具
点击右上角即可分享
微信分享提示