效果图:

主界面只有一个按钮就不上文件了

通知栏显示所用到的布局文件content_view.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#00000000"
  6. android:orientation="vertical"
  7. android:padding="5dp">
  8. <ImageView
  9. android:id="@+id/content_view_image"
  10. android:layout_width="25dp"
  11. android:layout_height="25dp"
  12. android:src="@drawable/logo"
  13. />
  14. <TextView
  15. android:id="@+id/content_view_text1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="0%"
  19. android:textColor="#000000"
  20. android:layout_toRightOf="@id/content_view_image"
  21. android:layout_centerHorizontal="true"
  22. android:layout_marginTop="5dp"
  23. android:layout_marginLeft="15dp"
  24. />
  25. <ProgressBar
  26. android:id="@+id/content_view_progress"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. style="@android:style/Widget.ProgressBar.Horizontal"
  30. android:max="100"
  31. android:layout_below="@id/content_view_image"
  32. android:layout_marginTop="4dp"
  33. />
  34. </RelativeLayout>

主运行类:

    1. package yyy.testandroid4;
    2. import java.util.Timer;
    3. import java.util.TimerTask;
    4. import android.app.Activity;
    5. import android.app.AlertDialog.Builder;
    6. import android.app.Notification;
    7. import android.app.NotificationManager;
    8. import android.app.PendingIntent;
    9. import android.content.DialogInterface;
    10. import android.content.Intent;
    11. import android.content.pm.PackageManager.NameNotFoundException;
    12. import android.os.Bundle;
    13. import android.os.Handler;
    14. import android.os.Message;
    15. import android.view.View;
    16. import android.view.View.OnClickListener;
    17. import android.widget.Button;
    18. import android.widget.RemoteViews;
    19. import android.widget.Toast;
    20. public class TestAndroid4Activity extends Activity {
    21. private Handler handler = new Handler(){
    22. @Override
    23. public void handleMessage(Message msg) {
    24. // TODO Auto-generated method stub
    25. super.handleMessage(msg);
    26. switch (msg.what) {
    27. case 0:
    28. notif.contentView.setTextViewText(R.id.content_view_text1, len+"%");
    29. notif.contentView.setProgressBar(R.id.content_view_progress, 100, len, false);
    30. manager.notify(0, notif);
    31. break;
    32. case 1:
    33. Toast.makeText(TestAndroid4Activity.this, "下载完成", 0).show();
    34. break;
    35. default:
    36. break;
    37. }
    38. }
    39. };
    40. private Button update,cancel;
    41. private int localVersion,serverVersion;
    42. private int len;
    43. private NotificationManager manager;
    44. private Notification notif;
    45. /** Called when the activity is first created. */
    46. @Override
    47. public void onCreate(Bundle savedInstanceState) {
    48. super.onCreate(savedInstanceState);
    49. setContentView(R.layout.main);
    50. update = (Button) findViewById(R.id.update);
    51. update.setOnClickListener(new OnClickListener() {
    52. @Override
    53. public void onClick(View arg0) {
    54. // TODO Auto-generated method stub
    55. //点击通知栏后打开的activity
    56. Intent intent = new Intent(TestAndroid4Activity.this,OtherActivity.class);
    57. PendingIntent pIntent = PendingIntent.getActivity(TestAndroid4Activity.this, 0, intent, 0);
    58. manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    59. notif = new Notification();
    60. notif.icon = R.drawable.logo;
    61. notif.tickerText = "新通知";
    62. //通知栏显示所用到的布局文件
    63. notif.contentView = new RemoteViews(getPackageName(), R.layout.content_view);
    64. notif.contentIntent = pIntent;
    65. manager.notify(0, notif);
    66. new DownLoadThread().start();
    67. }
    68. });
    69. }
    70. }
    71. private class DownLoadThread extends Thread{
    72. private Timer timer = new Timer();
    73. @Override
    74. public void run() {
    75. // TODO Auto-generated method stub
    76. super.run();
    77. timer.schedule(new TimerTask() {
    78. @Override
    79. public void run() {
    80. // TODO Auto-generated method stub
    81. Message msg = new Message();
    82. msg.what = 0;
    83. msg.obj = len;
    84. handler.sendMessage(msg);
    85. if(len == 100){
    86. timer.cancel();
    87. handler.sendEmptyMessage(1);
    88. }
    89. }
    90. }, 0, 1000);
    91. len = 0;
    92. try {
    93. while(len < 100){
    94. len++;
    95. Thread.sleep(1000);
    96. }
    97. } catch (InterruptedException e) {
    98. // TODO Auto-generated catch block
    99. e.printStackTrace();
    100. }
    101. }
    102. }
    103. }

android-带进度条的系统通知栏消息的更多相关文章

  1. 025 Android 带进度条的对话框(ProgressDialog)

    1.ProgressDialog介绍 ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待: 2.应用 ...

  2. Android带进度条的文件上传,使用AsyncTask异步任务

    最近项目中要做一个带进度条的上传文件的功能,学习了AsyncTask,使用起来比较方便,将几个方法实现就行,另外做了一个很简单的demo,希望能对大家有帮助,在程序中设好文件路径和服务器IP即可. A ...

  3. 高仿手机QQ音乐之——Android带进度条的开关

    最新版的手机QQ音乐体验确实不错,发现首页播放按钮能够显示歌曲当前进度条.认为挺有新意.效果例如以下: 自己琢磨了下.能够用自己定义组件来实现,试着做了一下.效果例如以下: 整理了下思路.大概设计流程 ...

  4. Android更新带进度条的通知栏

    在网上查询了下.Android版本号更新通知栏带进度条,醉了,基本都是复制过来.有的代码不全,连源代码下载都没有.有下载也须要积分,还不能用,真黑心啊!!之前自己也写过自己定义通知栏Notificat ...

  5. Android -- 自定义带进度条的按钮

    1. 实现了一个带进度条的按钮,完成后显示提示信息,并设置按钮为不可再次被点击

  6. [Delphi]带进度条的ListView

    带进度条的ListView unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, C ...

  7. android多线程进度条

    多线程实现更新android进度条. 实例教程,详细信息我已经注释   android多线程进度条   01package com.shougao.hello; 02 03import android ...

  8. 自定义带进度条的WebView , 增加获取web标题和url 回掉

    1.自定义ProgressWebView package com.app.android05; import android.content.Context; import android.graph ...

  9. android 自定义进度条颜色

    android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对Android自定义进度条的学习过程!   这个没法了只能看源码了,还好下载了源码, sources\b ...

随机推荐

  1. J2SE知识点摘记(一)

    1.        数组的声明时无法指定数组的长度. 2.        一维数组的声明和内存的分配 "数据类型    数组名[]; //声明一维数组     数组名=  new 数据类型[ ...

  2. Mono For Android中简单实现按钮的动画效果

    Android中动画的分Tween Animation和Frame Animation,本节主要讲Tween Animation的实现. 一般是通过XML文件来定义动画的,具体如下: 1.在项目res ...

  3. C#句柄使用

    原文:C#句柄使用 调用 API 函数 SendMessage 发送 WM_CLOSE 消息. DllImport("User32.dll",EntryPoint="Se ...

  4. gitlab 安装报错:Could not find modernizr-2.6.2 in any of the sources

      2014-04-30 15:27:44 标签:gitlab 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://davidbj.b ...

  5. 网易云课堂_程序设计入门-C语言_第六章:数组_1多项式加法

    1 多项式加法(5分) 题目内容: 一个多项式可以表达为x的各次幂与系数乘积的和,比如: 现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出. 程序要处理的 ...

  6. Tkinter类之窗口部件类

    Tkinter类之窗口部件类 Tkinter支持15个核心的窗口部件,这个15个核心窗口部件类列表如下:窗口部件及说明:Button:一个简单的按钮,用来执行一个命令或别的操作.Canvas:组织图形 ...

  7. n!的近似值 (stirling approximation)与 大O记法(big -O- notation)

    參考wiki: 1.n!的近似值 (stirling approximation) (中文) http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E9%9D ...

  8. Math.round(11.5)等于()Math.round(-11.5)等于()

    几天前去面试,这道简单的题目居然做错了,看来基础就是慢慢积累的.并不断使用和复习才会成为高手,假设基础不是那么熟练.恐怕在成为高手的路上会困难重重.所以在做项目的间歇时间.偶尔回顾一下最基础的知识.是 ...

  9. var_dump(php)

    var_dump -- 打印变量的相关信息 描述 void var_dump ( mixed expression [, mixed expression [, ...]] ) 此函数显示关于一个或多 ...

  10. wcf系列学习5天速成——第五天 服务托管

    今天是系列的终结篇,当然要分享一下wcf的托管方面的知识. wcf中托管服务一般有一下四种: Console寄宿:             利于开发调试,但不是生产环境中的最佳实践. winform寄 ...