栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

 
 
 
   
   
   
   
 

主运行类:

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; 
   
  @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(); 
      } 
    } 
     
  } 
   
   
} 

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160894.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号