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

主界面只有一個按鈕就不上文件了
通知欄顯示所用到的布局文件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();
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 中 EventBus 的使用之多線程事件處理
這篇文章主要介紹了Android 中 EventBus 的使用之多線程事件處理的相關(guān)資料,需要的朋友可以參考下2015-11-11
Activity實(shí)例詳解之啟動activity并返回結(jié)果
這篇文章主要介紹了Activity實(shí)例詳解之啟動activity并返回結(jié)果 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
android控件實(shí)現(xiàn)多張圖片漸變切換
這篇文章主要為大家詳細(xì)介紹了android控件實(shí)現(xiàn)多張圖片漸變切換,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Flutter使用AnimatedBuilder實(shí)現(xiàn)動效復(fù)用
Animation和AnimationWidget都是將組件和動畫一起完成的。有些時候,我們只是想動效復(fù)用,而不關(guān)心組件構(gòu)建,這個時候就可以使用 AnimatedBuilder了。本文詳細(xì)講解了AnimatedBuilder的使用,需要的可以參考一下2022-04-04
Android自定義View構(gòu)造函數(shù)詳解
這篇文章主要為大家詳細(xì)介紹了Android自定義View構(gòu)造函數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Android 4.4.2 橫屏應(yīng)用隱藏狀態(tài)欄和底部虛擬鍵的方法
這篇文章主要介紹了Android 4.4.2 橫屏應(yīng)用隱藏狀態(tài)欄和底部虛擬鍵的方法,需要的朋友可以參考下2017-01-01
詳解Android中PopupWindow在7.0后適配的解決
本篇文章主要介紹了詳解Android中PopupWindow在7.0后適配的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android 中ViewPager重排序與更新實(shí)例詳解
這篇文章主要介紹了Android 中ViewPager重排序與更新實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07

