Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android项目实现带进度条的系统通知栏消息大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

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

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://scheR_894_11845@as.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> 

主运行类:@H_607_1@

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,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); 
          } 
         
        } 
      },1000); 
      len = 0; 
      try { 
        while(len < 100){ 
          len++; 
          Thread.sleep(1000); 
        } 
      } catch (InterruptedException E) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
    } 
     
  } 
   
   
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的android项目实现带进度条的系统通知栏消息全部内容,希望文章能够帮你解决android项目实现带进度条的系统通知栏消息所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。