当前位置: 移动技术网 > 移动技术>移动开发>Android > android项目实现带进度条的系统通知栏消息

android项目实现带进度条的系统通知栏消息

2019年07月24日  | 移动技术网移动技术  | 我要评论

我们在做android开发的时候经常会遇到后台线程执行的比如说下载文件的时候,这个时候我们希望让客户能看到后台有操作进行,这时候我们就可以使用进度条,那么既然在后台运行,为的就是尽量不占用当前操作空间,用户可能还要进行其他操作,最好的方法就是在通知栏有个通知消息并且有个进度条。本文给一个例子工读者参考.
效果图如下:

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

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

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:background="#00000000" 
  android:orientation="vertical"  
  android:padding="5dp"> 
 
  <imageview  
    android:id="@+id/content_view_image" 
    android:layout_width="25dp" 
    android:layout_height="25dp" 
    android:src="@drawable/logo" 
     
    /> 
  <textview 
    android:id="@+id/content_view_text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="0%" 
    android:textcolor="#000000" 
    android:layout_torightof="@id/content_view_image" 
    android:layout_centerhorizontal="true" 
    android:layout_margintop="5dp" 
    android:layout_marginleft="15dp" 
   /> 
  <progressbar  
    android:id="@+id/content_view_progress" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    style="@android:style/widget.progressbar.horizontal" 
    android:max="100" 
    android:layout_below="@id/content_view_image" 
    android:layout_margintop="4dp" 
    /> 
   
</relativelayout> 

主运行类:

package yyy.testandroid4; 
 
import java.util.timer; 
import java.util.timertask; 
 
import android.app.activity; 
import android.app.alertdialog.builder; 
import android.app.notification; 
import android.app.notificationmanager; 
import android.app.pendingintent; 
import android.content.dialoginterface; 
import android.content.intent; 
import android.content.pm.packagemanager.namenotfoundexception; 
import android.os.bundle; 
import android.os.handler; 
import android.os.message; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.widget.button; 
import android.widget.remoteviews; 
import android.widget.toast; 
 
public class testandroid4activity extends activity { 
   
   
  private handler handler = new handler(){ 
    @override 
    public void handlemessage(message msg) { 
      // todo auto-generated method stub 
      super.handlemessage(msg); 
      switch (msg.what) { 
      case 0: 
        notif.contentview.settextviewtext(r.id.content_view_text1, len+"%"); 
        notif.contentview.setprogressbar(r.id.content_view_progress, 100, len, false); 
        manager.notify(0, notif); 
         
        break; 
      case 1: 
        toast.maketext(testandroid4activity.this, "下载完成", 0).show(); 
        break; 
      default: 
        break; 
      } 
    } 
     
  }; 
   
  private button update,cancel; 
  private int localversion,serverversion; 
  private int len; 
  private notificationmanager manager; 
  private notification notif; 
  /** called when the activity is first created. */ 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
     
    update = (button) findviewbyid(r.id.update); 
    
    update.setonclicklistener(new onclicklistener() { 
      @override 
      public void onclick(view arg0) { 
        // todo auto-generated method stub 
        //点击通知栏后打开的activity 
        intent intent = new intent(testandroid4activity.this,otheractivity.class); 
         
        pendingintent pintent = pendingintent.getactivity(testandroid4activity.this, 0, intent, 0); 
           manager = (notificationmanager) getsystemservice(notification_service); 
        notif = new notification(); 
        notif.icon = r.drawable.logo; 
        notif.tickertext = "新通知"; 
        //通知栏显示所用到的布局文件 
        notif.contentview = new remoteviews(getpackagename(), r.layout.content_view); 
        notif.contentintent = pintent; 
        manager.notify(0, notif); 
        new downloadthread().start(); 
      } 
    }); 
     
     
     
  } 
 } 
   
  private class downloadthread extends thread{ 
    private timer timer = new timer(); 
    @override 
    public void run() { 
      // todo auto-generated method stub 
      super.run(); 
      timer.schedule(new timertask() { 
        @override 
        public void run() { 
          // todo auto-generated method stub 
           
          message msg = new message(); 
          msg.what = 0; 
          msg.obj = len; 
          handler.sendmessage(msg); 
           
          if(len == 100){ 
            timer.cancel(); 
            handler.sendemptymessage(1); 
          } 
         
        } 
      }, 0, 1000); 
      len = 0; 
      try { 
        while(len < 100){ 
          len++; 
          thread.sleep(1000); 
        } 
      } catch (interruptedexception e) { 
        // todo auto-generated catch block 
        e.printstacktrace(); 
      } 
    } 
     
  } 
   
   
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网