zoukankan      html  css  js  c++  java
  • 通知栏消息(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>
  • 相关阅读:
    CSS媒体查询
    搜索关键词标注红色
    揭秘 | 小白如何0基础0元建站
    细说浏览器输入URL后发生了什么
    js问题总结
    vue elementui如何修改el-table头部样式
    h5开发微信公众号重定向到关注页面没有关注按钮 (微信你个坑)
    下拉展开动画
    html中常用的转义字符总结
    9个设计师常用的高清图库 不敢配图? 这9个免版权图库牢记心中!
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/3860257.html
Copyright © 2011-2022 走看看