要实现Android的消息提示,就是提示用户有新的更新或者信息。即使当用户退出了应用程序。也会类似短信提示那种功能。长话短说,开个service,在后台一直跑,进行数据监控,发现有更新就触发消息提示功能。

上代码:

package com.example.notificationdemo;import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RemoteViews;public class MainActivity extends Activity {Button start;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);start = (Button) findViewById(R.id.start);start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// showDefaultNotification();showCustomizeNotification();}});}// 自定义显示的通知 ,创建RemoteView对象private void showCustomizeNotification() {CharSequence title = "信息";int icon = R.drawable.icon_blue;long when = System.currentTimeMillis();Notification noti = new Notification(icon, title, when + 10000);// noti.flags = Notification.FLAG_INSISTENT;noti.defaults |= Notification.DEFAULT_SOUND;noti.defaults |= Notification.DEFAULT_VIBRATE;noti.flags |= Notification.FLAG_AUTO_CANCEL;noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;// 1、创建一个自定义的消息布局 view.xml// 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification);remoteView.setImageViewResource(R.id.noti_icon, R.drawable.icon_blue);remoteView.setTextViewText(R.id.noti_content, "出现预警,请查实!");noti.contentView = remoteView;// 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)// 这儿点击后简单启动Settings模块PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent("android.settings.SETTINGS"),0);noti.contentIntent = contentIntent;NotificationManager mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mnotiManager.notify(0, noti);}// 默认显示的的Notificationprivate void showDefaultNotification() {// 定义Notication的各种属性CharSequence title = "i am new";int icon = R.drawable.icon_blue_small;long when = System.currentTimeMillis();Notification noti = new Notification(icon, title, when + 10000);noti.flags = Notification.FLAG_INSISTENT;// 创建一个通知Notification mNotification = new Notification();// 设置属性值mNotification.icon = R.drawable.icon_blue_small;mNotification.tickerText = "NotificationTest";mNotification.when = System.currentTimeMillis(); // 立即发生此通知// 带参数的构造函数,属性值如上// Notification mNotification = = new// Notification(R.drawable.icon,"NotificationTest",// System.currentTimeMillis()));// 添加声音效果mNotification.defaults |= Notification.DEFAULT_SOUND;// 添加震动,后来得知需要添加震动权限 : Virbate Permission// mNotification.defaults |= Notification.DEFAULT_VIBRATE ;// 添加状态标志// FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉// FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉// FLAG_ONGOING_EVENT 通知放置在正在运行// FLAG_INSISTENT 通知的音乐效果一直播放mNotification.flags = Notification.FLAG_INSISTENT;// 将该通知显示为默认ViewPendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent("android.settings.SETTINGS"),0);mNotification.setLatestEventInfo(MainActivity.this, "通知类型:默认View","一般般哟。。。。", contentIntent);// 设置setLatestEventInfo方法,如果不设置会App报错异常NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 注册此通知// 如果该NOTIFICATION_ID的通知已存在,会显示最新通知的相关信息 ,比如tickerText 等mNotificationManager.notify(2, mNotification);}private void removeNotification() {NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 取消的只是当前Context的NotificationmNotificationManager.cancel(2);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}

配套demo: http://download.csdn.net/detail/vaecer/8416543

更多相关文章

  1. Android studio使用过程中提示“警告: [deprecation] android.ha
  2. Android中检测更新、通知、下载等
  3. android通知栏快捷设置开发,即添加快捷磁贴指北
  4. listView背景问题以及限制editText字数以及如果想通知别人已经不
  5. Android导入一个工程时提示 Invalid project description
  6. Android开发时提示Your project contains error(s),please fix t
  7. Android消息提示:AlertDialog、Toast、Notification的使用
  8. android 引导用户指示操作 高亮显示 可以自定义文字或者图片来作

随机推荐

  1. Android实现拍照或从相册中获取图片并且
  2. 关于Unity发布苹果并真机测试的设置与连接
  3. Android中实现native服务利用binder与应
  4. Android的签名文件生成两种方法
  5. 避免Drawable保持引用的内存泄露
  6. android textview 排版混乱问题
  7. Android怎么解决不同版本SDK的兼容性问题
  8. php运行过程和数据类型
  9. Android进阶——借助Loader机制以一种更
  10. Android的事件处理