您的位置:首页 > 移动开发 > Android开发

android中notification通知及自定义notification

2013-08-10 21:15 405 查看




//               (写的顺序2) 1.创建通知对象
//                有通知的图标R.drawable.ic_launcher
Notification notification = new Notification(R.drawable.ic_launcher,
"通知1", System.currentTimeMillis());//当前时间
notification.flags = Notification.FLAG_AUTO_CANCEL;//自动取消通知(点击完)

//               (4) 2.点击事件关联的Activity
//跳转到SecondActivity页面
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
PendingIntent contentIntent = PendingIntent.
getActivity(MainActivity.this, 0, intent, 0);
//                3.设置通知的标题和内容
//                notification设置一个事件处理
notification.setLatestEventInfo(MainActivity.this, "title",
"message", contentIntent);
//               (1) 4.发送通知
//得到通知管理器
//                NotificationManager notificationManager = (NotificationManager)
//                        getSystemService(Context.NOTIFICATION_SERVICE);
//用通知管理器发通知
notificationManager.notify(1234, notification);


//                notificationManager.cancel(1234);//手动清除通知


自定义notification及进度条更新的方法:




@Override
public void onClick(View v)
{
sendNotifi();
for (int i = 1; i <= 10; i++)
{
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
updateNotifi(i);
}
finishNotifi();
}
private void finishNotifi()
{
notification = new Notification(R.drawable.ic_launcher, "下载完成", System.currentTimeMillis());
//设置通知的声音
notification.flags |= Notification.DEFAULT_SOUND;
Intent intent = new Intent();
notification.contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
//使用自定义的布局
notification.contentView = new RemoteViews(getPackageName(), R.layout.notify);
notification.contentView.setTextViewText(R.id.textView1, "进度100%");
notification.contentView.setProgressBar(R.id.progressBar1, 10, 10, false);
mNotifimgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifimgr.notify(1234, notification);
}
private void updateNotifi(int i)
{
notification.contentView.setTextViewText(R.id.textView1, "进度");
notification.contentView.setProgressBar(R.id.progressBar1, 10, i, false);
mNotifimgr.notify(1234,notification);
}
private void sendNotifi()
{
notification = new Notification(R.drawable.ic_launcher, "准备升级", System.currentTimeMillis());
//设置通知的声音
notification.flags |= Notification.DEFAULT_SOUND;
Intent intent = new Intent();
notification.contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.contentView = new RemoteViews(getPackageName(), R.layout.notify);
mNotifimgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifimgr.notify(1234, notification);
}


本文出自 “wangcuijing” 博客,请务必保留此出处http://wangcuijing.blog.51cto.com/7233352/1269485
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐