码迷,mamicode.com
首页 > 其他好文 > 详细

通知栏消息(Notification)初步

时间:2014-07-22 22:40:33      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   color   

bubuko.com,布布扣  bubuko.com,布布扣

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)初步,布布扣,bubuko.com

通知栏消息(Notification)初步

标签:android   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/tianzhijiexian/p/3860257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!