編輯:關於 Android 程式設計
在做 Android 開發時,我們常常會遇到後台線程執行耗時任務的情況,比如下載文件。這時如果能讓用戶直觀地看到「後台正在忙」,體驗會好很多——而進度條正是最合適的載體。由於任務在後台運行,目的就是盡量不佔用當前操作界面,讓用戶能繼續做別的事,所以把進度條放進通知欄是再合適不過的選擇。本文就用一個完整例子演示這種做法。
效果圖如下:

主界面只有一個按鈕,這裡就不單獨貼布局了。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://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" 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(); } } } }
以上就是本文的全部內容,希望對你的學習有所幫助。
FragmentTabHost 的使用方式與布局重點
在需要底部切換多個 Fragment 的頁面中,FragmentTabHost 是一個很方便的容器。它能管理多個 Tab,並在切換時掛載對應的 Fragment,適合主頁
Android 官方 SwipeRefreshLayout 下拉刷新實戰
這是一個基於最新 v4 包實現的下拉刷新示例。整體非常簡單,畢竟用的是官方組件而非自定義,大家可以放寬心。廢話不多說,直接上代碼。 Activity 代碼 讓 Acti
Android 入門實作:做一個人品計算器,學會 Activity 間資料傳遞
這個小專案看起來只是玩笑,但它把幾個入門關鍵都練到了:控制項取值、表單驗證、Toast 提示、显式 Intent 跳轉、以及用 putExtra()/getXxxExtr
Android 創建與引用 Library Project(庫項目)完整教程
開發過程中,为了实现某個陌生的效果或功能,我們常常會求助於網上優秀的開源項目。使用時會發現,引用開源的 Library Project(庫項目)確實能解決不少問題,也提供