现在的位置: 首页 > 综合 > 正文

————————-android-带进度条的系统通知栏消息

2018年03月20日 ⁄ 综合 ⁄ 共 5542字 ⁄ 字号 评论关闭

http://blog.csdn.net/centralperk/article/details/7550294

效果图:

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

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

[java] view
plain
copy

  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.   
  9.     <ImageView   
  10.         android:id="@+id/content_view_image"  
  11.         android:layout_width="25dp"  
  12.         android:layout_height="25dp"  
  13.         android:src="@drawable/logo"  
  14.           
  15.         />  
  16.     <TextView  
  17.         android:id="@+id/content_view_text1"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="0%"  
  21.         android:textColor="#000000"  
  22.         android:layout_toRightOf="@id/content_view_image"  
  23.         android:layout_centerHorizontal="true"  
  24.         android:layout_marginTop="5dp"  
  25.         android:layout_marginLeft="15dp"  
  26.       />  
  27.     <ProgressBar   
  28.         android:id="@+id/content_view_progress"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         style="@android:style/Widget.ProgressBar.Horizontal"  
  32.         android:max="100"  
  33.         android:layout_below="@id/content_view_image"  
  34.         android:layout_marginTop="4dp"  
  35.         />  
  36.       
  37. </RelativeLayout>  


主运行类:

[java] view
plain
copy

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

抱歉!评论已关闭.