国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息

2019-12-12 04:51:36
字體:
供稿:網(wǎng)友

我們?cè)谧鯝ndroid開發(fā)的時(shí)候經(jīng)常會(huì)遇到后臺(tái)線程執(zhí)行的比如說下載文件的時(shí)候,這個(gè)時(shí)候我們希望讓客戶能看到后臺(tái)有操作進(jìn)行,這時(shí)候我們就可以使用進(jìn)度條,那么既然在后臺(tái)運(yùn)行,為的就是盡量不占用當(dāng)前操作空間,用戶可能還要進(jìn)行其他操作,最好的方法就是在通知欄有個(gè)通知消息并且有個(gè)進(jìn)度條。本文給一個(gè)例子工讀者參考.
效果圖如下:

主界面只有一個(gè)按鈕就不上文件了

通知欄顯示所用到的布局文件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> 

主運(yùn)行類:

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         //點(diǎn)擊通知欄后打開的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();       }     }        }       } 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙陵县| 嵩明县| 广平县| 乾安县| 广德县| 宁波市| 阳朔县| 锡林郭勒盟| 锦州市| 郯城县| 徐闻县| 罗山县| 德化县| 额济纳旗| 台山市| 公主岭市| 迭部县| 安乡县| 津南区| 马山县| 类乌齐县| 舒兰市| 杭锦旗| 荆州市| 嘉峪关市| 宝坻区| 乌鲁木齐县| 蚌埠市| 周口市| 北宁市| 仁布县| 钟山县| 耒阳市| 铁岭县| 沁源县| 监利县| 梨树县| 香河县| 彰武县| 宣武区| 印江|