Android 实现任务栏出现类似短信的提示功能
2015-02-02 13:55
148 查看
要实现Android的消息提示,就是提示用户有新的更新或者信息。即使当用户退出了应用程序。也会类似短信提示那种功能。长话短说,开个service,在后台一直跑,进行数据监控,发现有更新就触发消息提示功能。
上代码:
配套demo:http://download.csdn.net/detail/vaecer/8416543
上代码:
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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.start); start.setOnClickListener(new OnClickListener() { @Override public 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); } // 默认显示的的Notification private 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; // 将该通知显示为默认View PendingIntent 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的Notification mNotificationManager.cancel(2); } @Override public 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
相关文章推荐
- Android-ListView实现类似Gallery的功能
- 使用FlexBox和Json实现类似ComboBox(类似Google的输入提示和自动)功能-基于JQuery-ASP.NET
- 举例说明android中SmsManager的用法---用来实现手机发送短信的功能
- [120_移动开发Android]002_android基础功能实现之短信功能
- Android 图片裁剪功能实现详解(类似QQ自定义头像裁剪)
- 如何用android实现类似windows的画板功能
- Android 图片裁剪功能实现详解(类似QQ自定义头像裁剪) 推荐
- android-类似微信和米聊中摇一摇找好友的功能的实现
- Android短信功能实现源码
- Android 图片裁剪功能实现详解(类似QQ自定义头像裁剪)
- android AutoCompleteTextView 实现输入提示,类似百度支持输入拼音提示中文(gray)
- Dhtml+正则实现类似IDE编辑器的动态提示输入功能
- android实现发送短信的功能
- 实现 Android 通知提示功能
- android实现图片右上方出现数字,类似邮箱的未读邮件效果
- jquery实现搜索框类似提示功能(改进)
- 在Winfrom下实现类似百度、Google搜索自能提示功能
- ASP.NET中实现类似MSN消息提示功能
- Android 图片裁剪功能实现详解(类似QQ自定义头像裁剪)
- android之垃圾短信拦截功能实现