NotificationActivity.java:
package com.demo.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.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.Builder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews; import com.demo.broadcast.R; /** * @ClassName: NotificationActivity * @Description: 通知测试demo * @author chenzheng * @date 2014-9-5 下午2:54:14 */ public class NotificationActivity extends Activity implements OnClickListener{ private Button btn_show_intent_act, btn_show_custom; /** Notification构造器 */ private NotificationCompat.Builder mBuilder; /** Notification的ID */ private int notifyId = 100; /** Notification管理 */ private NotificationManager mNotificationManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification); initService(); btn_show_intent_act = (Button) findViewById(R.id.btn_show_intent_act); btn_show_intent_act.setOnClickListener(this); btn_show_custom = (Button) findViewById(R.id.btn_show_custom); btn_show_custom.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_show_intent_act: showIntentActivityNotify(); break; case R.id.btn_show_custom: showCustomNotify(); break; default: break; } } /** 显示通知栏点击跳转到指定Activity */ public void showIntentActivityNotify(){ mBuilder = new NotificationCompat.Builder(this); mBuilder.setAutoCancel(true)//点击后让通知将消失 .setContentTitle("测试标题") .setContentText("点击跳转") .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) .setTicker("点我") .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示 .setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级 .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接) .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合: //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission .setSmallIcon(R.drawable.ic_launcher); //点击的意图ACTION是跳转到Intent Intent resultIntent = new Intent(this, NotificationActivity.class); resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendingIntent); mNotificationManager.notify(notifyId, mBuilder.build()); } /** 显示自定义通知*/ public void showCustomNotify(){ //先设定RemoteViews RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom); //设置对应IMAGEVIEW的ID的资源图片 view_custom.setImageViewResource(R.id.custom_icon, R.drawable.ic_launcher); view_custom.setTextViewText(R.id.tv_custom_title, "淘宝放价"); view_custom.setTextViewText(R.id.tv_custom_content, "欢乐中秋,淘宝大放价,千万豪礼等你来拿。"); mBuilder = new Builder(this); mBuilder.setContent(view_custom) .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示 .setTicker("有新消息") .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级 .setOngoing(false)//不是正在进行的 true为正在进行 效果和.flag一样 .setSmallIcon(R.drawable.ic_launcher); Notification notify = mBuilder.build(); notify.contentView = view_custom; mNotificationManager.notify(notifyId, notify); } /** * 初始化要用到的系统服务 */ private void initService() { mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } /** * @获取默认的pendingIntent,为了防止2.3及以下版本报错 * @flags属性: * 在顶部常驻:Notification.FLAG_ONGOING_EVENT * 点击去除: Notification.FLAG_AUTO_CANCEL */ public PendingIntent getDefalutIntent(int flags){ PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags); return pendingIntent; } }
notification.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_show_intent_act" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示通知,点击跳转到指定Activity" /> <Button android:id="@+id/btn_show_custom" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示自定义通知" /> </LinearLayout>
view_custom.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/custom_icon" android:layout_width="50dip" android:layout_height="50dip" android:layout_alignParentLeft="true" android:layout_margin="5dip" android:padding="5dip" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_toRightOf="@id/custom_icon" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_custom_title" style="@style/NotificationTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="title" android:textSize="15sp" /> <TextView android:id="@+id/tv_custom_time" style="@style/NotificationTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="16.49" android:textSize="12sp" /> </RelativeLayout> <TextView android:id="@+id/tv_custom_content" style="@style/NotificationContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="1dip" android:text="content" android:textSize="12sp" /> </LinearLayout> </RelativeLayout>
添加允许震动权限:
<uses-permission android:name="android.permission.VIBRATE" />
相关推荐
Android 通知(notification)简单实用Demo,包含点击功能简单例子,供大家下载学习
这个是android notification框架,如有需要请下载。发发发
自学 Android 做的第二十七个 Android 程序,使用了 NotificationManager、Notification 和 Notification.Builder
这个是通知栏框架(Notificaiton)的全面学习,里面把大概所有的情况都列了出来,通过一个DEMO让你了解它的大致所有使用过程。 可以通过以下博文进行配套了解(有效果图): ...
Anroid,一个简单的带按钮自定义通知栏,欢迎下载试用,人人为我,我为人人,共同学习,共同进步
这个是通知栏框架(Notificaiton)的全面学习,里面把大概所有的情况都列了出来,通过一个DEMO让你了解它的大致所有使用过程。 可以通过以下博文进行配套了解(有效果图): ...
本实例描述的Android开发中的Notification通知控件的应用,包括通知的创建、属性设置、跳转方法以及删除等。希望能给学习Notification通知的朋友带来帮助。
NULL 博文链接:https://forlan.iteye.com/blog/2264722
一个多功能方便好用的notification通知栏通知开源库.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
使用android做的小例子 可以在通知栏显示消息和小图标 点击进去可以进入一个页面 主要用于notification的学习 基于android2.3开发
Android应用源码开发Demo,主要用于毕业设计学习。
Android应用源码开发Demo,主要用于毕业设计学习。
Notification通知DEMO 仅供大家学习
状态栏通知小图标,notification通知.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
对话框通知是一种可以与用户进行交互的通知形式,会使当前的活动失焦。 AlertDialog(警告对话框) 用于创建警告对话框。一个警告对话框最多可以包含三个按钮(和/或)一个包含复选框或单选按钮的可选项列表。 使用...
主要给大家介绍了关于Android中Notification机制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
1 1 4 如何解决Android学习中遇到的难题 1 1 5 Android只能在手机或其他移动设备上使用吗 1 2 Android的基本概念 1 2 1 Android的特点都有哪些 1 2 2 MVC模式 1 2 3 Android 的系统构架 1 3 开发Android应用程序 1 3...