效果图:

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

通知栏显示所用到的布局文件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. }

最新文章

  1. 初步了解nodejs
  2. java高新技术-代理
  3. C#路径/文件/目录/I/O常见操作汇总
  4. javascript之DOM篇二(操作)
  5. ARC Rules
  6. 9. shell环境
  7. Servlet页面跳转实现方法的区别
  8. MyEclipse中消除frame引起的“the file XXX can not be found.Please check the location and try again.”的错误
  9. 开源yYmVc项目 v 0.2 版本号介绍
  10. Lumen 配置、重写、404错误等
  11. 【JS】cookies 的使用
  12. MYSQL的学习
  13. maven中jar下载失败
  14. NIO相关
  15. xl2tpd[26104]: Maximum retries exceeded for tunnel 33925. Closing
  16. Python3.6连接mysql(一)
  17. docker4种网络最佳实战 --摘自https://www.cnblogs.com/iiiiher/p/8047114.html
  18. byteify
  19. 关于网站中Logo部分的写法
  20. RabbitMQ:基本命令

热门文章

  1. 您在基于 Windows 7 的或基于 Windows Server 2008 R2 的计算机上读取器中插入智能卡时出现错误消息:&quot;设备驱动程序软件未能成功安装&quot;
  2. ArcGIS学习推荐基础教程摘录
  3. linux中ctrl+z、ctrl+d和ctrl+c的区别
  4. Linux学习之十一、环境变量的功能
  5. SQL Server中的DATEPART函数的使用
  6. 使用SuperWebSocket 构建实时 Web 应用
  7. send js object to webapi or mvc
  8. 配置Nutch模拟浏览器以绕过反爬虫限制
  9. sqlserver查询某一字段重复超5次的所有记录
  10. 关于新装ubuntu系统update失败和build-essential失败的解决办法