欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android項目實現帶進度條的系統通知欄消息

 更新時間:2016年10月31日 17:09:37   作者:Central-Perk  
本篇文章主要介紹了android項目實現帶進度條的系統通知欄消息,就是實現在通知欄看到下載進度。具有一定的參考價值,感興趣的小伙伴們可以參考一下。

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

主界面只有一個按鈕就不上文件了

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

主運行類:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Flutter 創(chuàng)建私有公共插件的步驟

    Flutter 創(chuàng)建私有公共插件的步驟

    flutter pub中有很多開源庫,如dio、provider等,這些都是package,直接在pubspec中引入就可以在工程中使用??聪氯绾巫约哼M行創(chuàng)建
    2021-05-05
  • Android控件gridview實現單行多列橫向滾動效果

    Android控件gridview實現單行多列橫向滾動效果

    這篇文章主要為大家詳細介紹了Android控件gridview實現單行多列橫向滾動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android應用內懸浮窗的實現方案示例

    Android應用內懸浮窗的實現方案示例

    本篇文章主要介紹了Android應用內懸浮窗的實現方案示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Android自定義照相機Camera出現黑屏的解決方法

    Android自定義照相機Camera出現黑屏的解決方法

    這篇文章主要介紹了Android自定義照相機Camera出現黑屏的解決方法,分析了黑屏出現的原因及參考解決方法,需要的朋友可以參考下
    2016-08-08
  • Android listview的滑動沖突解決方法

    Android listview的滑動沖突解決方法

    這篇文章主要介紹了Android listview的滑動沖突解決方法的相關資料,需要的朋友可以參考下
    2017-02-02
  • Android獲取手機配置信息具體實現代碼

    Android獲取手機配置信息具體實現代碼

    下面為大家介紹下使用android獲取手機配置信息的具體過程,感興趣的朋友可以參考下哈,希望對你有所幫助
    2013-06-06
  • Android?studio開發(fā)實現計算器功能

    Android?studio開發(fā)實現計算器功能

    這篇文章主要為大家詳細介紹了Android?studio開發(fā)實現計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • PagerSlidingTabStrip制作Android帶標簽的多界面滑動切換

    PagerSlidingTabStrip制作Android帶標簽的多界面滑動切換

    這篇文章主要介紹了使用PagerSlidingTabStrip制作Android帶標簽的多界面滑動切換效果的方法,PagerSlidingTabStrip是GitHub上的一個開源項目,調用這個庫可以少寫不少代碼XD 需要的朋友可以參考下
    2016-04-04
  • Android中Activity組件實例介紹

    Android中Activity組件實例介紹

    大家好,本篇文章主要講的是Android中Activity組件實例介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android?Hilt?Retrofit?Paging3使用實例

    Android?Hilt?Retrofit?Paging3使用實例

    這篇文章主要介紹了Android?Hilt依賴注入的使用,首先,某個類的成員變量稱為依賴,如若此變量想要實例化引用其類的方法,可以通過構造函數傳參或者通過某個方法獲取對象,此等通過外部方法獲取對象實例的稱為依賴注入
    2023-01-01

最新評論