- 浏览: 512002 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
u010169720:
而且fragment调用back也有不需要finish的时候呢 ...
android异常:Can not perform this action after onSaveInstanc -
u010169720:
我这里就是拦截的,但是还是报了这个
android异常:Can not perform this action after onSaveInstanc -
Salijing:
IT之冕 写道 我的方法是注释掉super.onBackPre ...
android异常:Can not perform this action after onSaveInstanc -
cainiao1923:
上线毛啊,Bad request了
基于NodeJs开发的独立博客系统~~上线啦 -
cainiao1923:
不错 !!!!!!!!!!!!!!
在Android上实现Java和Js交互
多任务机制下,程序跑在后台,用户使用服务,形成了一个良好的使用结构。程序运行的状态或结果,在不干扰用户当前使用的状态下,以通知的方式通知用户,能大大提升地用户的使用感。
在android下,与通知功能相关的组件大致有以下三大部分:
android.app.PendingIntent; android.app.NotificationManager; android.app.Notification;
NotificationManager可以通过系统的服务获取,我就是通过它向用户发出通知的
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification,通知的主体,通过设置其各种属性,来实现通知的效果,
PendingIntent,Intent的封装类,主要是用来导向点击通知后要启动的Activity。不过这里主要牵涉到Intent属性的设置,以下两段代码,前者跳转Activity时会新建一个Activity的实例,后者则会先从栈堆里弹出
intent = new Intent(NotificationTest.this, NotificationTest.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); pendIntent = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0); notif.contentIntent = pendIntent;
intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(NotificationTest.this, NotificationTest.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); pendIntent = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0);
android下的通知组件使用起来很简单,下面是自己的一个例子,例子模拟下载的通知,并使用Timer和Handle一步更新通知上的进度条:
public class NotificationTest extends Activity { private NotificationManager notificationManager; private String svcName = Context.NOTIFICATION_SERVICE; private Intent intent; private PendingIntent pendIntent; private Notification notif; //通知的变量 private RemoteViews rv = null; //自定义通知的外观 private final int NOFI_REF = 1; //通知的标志 private TextView tv = null; private Timer timer = null; private int number = 0; private int num = 0; private boolean isOver = true; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.myTv); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if(number!=0){ tv.setText("down again! \n"); } if (timer == null && isOver==true) { setNotification(); timer = new Timer(); timer.schedule(new MyTask(), 1000); isOver = false; } } }); tv.setText("Tis is Notification Test! \n Click screen to start! \n"); //获取系统的通知服务管理器实例 notificationManager = (NotificationManager) getSystemService(svcName); } private void setNotification() { long when = System.currentTimeMillis(); notif = new Notification(android.R.drawable.ic_media_rew, "Test Notification", when); //new一个通知的实例,并设置在状态栏的显示方式 // 点击通知时转移的内容 intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(NotificationTest.this, NotificationTest.class); //设置intent的标志,以控制目标Activity的启动方式 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); pendIntent = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0); notif.contentIntent = pendIntent; // //notif.defaults = Notification.DEFAULT_SOUND; //设置通知的声音,也可以通过uri属性自定义 long[] ss = {1000,500,1000}; notif.vibrate = ss; //设置通知时震动 rv = new RemoteViews(this.getPackageName(), R.layout.my_notification_view); //设置通知自定义的外观 notif.contentView = rv; // //设置进度条的最大进度和初始进度 notif.contentView.setProgressBar(R.id.pb, 100, 10, false); //显示下载的内容 notif.contentView.setTextViewText(R.id.tv_name, "正在下载"); //显示下载的进度 notif.contentView.setTextViewText(R.id.tv_p, "0%"); notif.flags = Notification.FLAG_ONGOING_EVENT; //设置通知的标志,定义在通知栏上的显示方式 notificationManager.notify(NOFI_REF, notif); //向系统发出通知 } class MyTask extends TimerTask { @Override public void run() { Message msg = new Message(); num+=5; Bundle b = new Bundle(); b.putInt("pb", num); msg.setData(b); msg.what = 1; handeler.sendMessage(msg); } }; private Handler handeler = new Handler(){ public void handleMessage(Message msg) { if(msg.what==1){ Bundle b =msg.getData(); int i = b.getInt("pb"); if(i<100){ tv.setText(tv.getText() + "Notification update:" + i + "\n"); timer.cancel(); timer = null; if(i%10==0){ rv.setProgressBar(R.id.pb, 100, i, false); //更新进度条 rv.setTextViewText(R.id.tv_p, i+"%"); //更新label的值 notif.vibrate = null; //关掉震动 notif.defaults = 0; //关掉声音 notificationManager.notify(NOFI_REF, notif); //发出通知,更新通知栏上的状态 } timer = new Timer(); timer.schedule(new MyTask(), 500); } else { timer.cancel(); timer = null; //notif.contentView.removeAllViews(R.id.progress_lay); //去掉进度条 //notif.contentView.setTextViewText(R.id.tv_name, "下载完成"); //更新显示的信息 notif.setLatestEventInfo(NotificationTest.this, "下载完成", "OK!", pendIntent); //如方法名称一样,设置要通知的最新事件信息 //要注意的是,绑定通知的contentView和通知的setLatestEventInfo方法, //两者只能选一种,否则通知只会展现最后绑定的效果 notif.flags = Notification.FLAG_AUTO_CANCEL; //设置通知在用户点击后就会自动取消 long[] ss = {1000,500,1000}; notif.vibrate = ss; notif.number = ++number; //设置通知的条数 notificationManager.notify(NOFI_REF, notif); //发出通知,更新通知栏上的状态 isOver = true; num = 0; } } } }; }
发表评论
-
【翻译】 Android上实现文字围绕图片的布局
2014-04-09 16:31 3340在这边文章中,我将会介绍如何去实现一个在An ... -
【翻译】Android多线程下安全访问数据库
2014-04-08 11:21 11423为了记录如何线程安全地访问你的Android数 ... -
Android Robotium小结
2014-03-17 15:41 2291Robotium 是一款基于Android instrum ... -
Monkeyrunner小结
2014-03-15 23:49 6475Monkeyrunner----Android tools ... -
Android开发陷阱:利用PendingIntent传递唯一的Intent
2013-11-07 11:36 42090PendingIntent 是对真实 ... -
Android开发陷阱:利用PendingIntent传递唯一的Intent
2013-11-07 11:29 0PendingIntent 是对真实 ... -
GridView的特殊派生应用
2013-11-06 17:07 2123一、ScrollView + GridView 在Sc ... -
Android的ps命令介绍和技巧
2013-07-05 18:37 6612在Linux下,ps是非常普 ... -
在Android上实现Java和Js交互
2013-07-05 18:34 28165其实webview加载资源的速度并不慢,但是如果资源多 ... -
TextView的日常使用技巧
2013-07-05 18:29 5002Textview在日常开发工作中使用非常频繁,在这里 ... -
Android NDK开发之Jni调用Java对象
2013-03-16 00:11 26312本地代码中使用Java对 ... -
Android NDK开发之数组类型的操作
2013-03-16 00:05 8800Jni 可以通过JNIEnv提供的方法,对传过来的Java ... -
Android NDK开发之Jni的数据类型
2013-03-16 00:03 11345在前面的一篇博客《And ... -
Android NDK开发简介
2013-03-07 15:12 5878最近由于项目的 ... -
Android的NDK开发~SQLite3
2013-02-05 14:02 0What? NDK的N,即native,NDK开发,是指使 ... -
Android的NDK开发~Hellow world!
2013-02-05 13:52 24251、到Google官网下载adt- ... -
关于SD卡管理的注意事项
2013-01-18 11:24 11870问题: 本人一直有使 ... -
Activity流程控制之startActivityForResult
2012-10-18 09:48 0startActivityForResult不能与flag I ... -
Json解析之Gson库
2012-09-24 11:46 25525Json(JavaScript Object Notation ... -
搞定个性化输入提示--自定义AutoCompleteTextView
2012-09-04 14:44 5015在设计app的输入界面时,假如假如智能提示,给用户带来的不只是 ...
相关推荐
Notification
Notification最新用法、实现Notification的通知栏常驻、Notification的各种样式big View、解决Notification点击无效
疯狂Android中有关Notification的简单例子
使用javascript 封装实现 Notification 消息通知框
各种Notification的使用技巧
示例功能包含普通Notification、折叠式Notification和悬挂式Notification,并且实现了Notification的显示等级。
android notification 通知 demo
调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotifacationManager服务 通过构造器创建一个Notification对象 为Notification设置各种属性 通过NotificationManager发送Notification。
Android NOtification 使用Android NOtification 使用
Bluetooth Alert Notification Profile
这个例子演示Android 在状态栏添加Notification信息图标及提示,相信大家对这个功能已经不陌生了,手机中安装的APP,一般都会在后台运行,时不时会在手机顶部的状态栏中显示应用的图标,滑出状态栏会看到详细的信息...
Notification的使用示例各种效果,包含系统一般notification的示例,以及自定义铃声震动等
android notification完全解析Demo
iphone notification机制导读。理解notification的工作机理,不仅仅局限于简单使用。
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
通过RemoteView制作自定义Notification
Notification显示音乐下载的界面