免费高清特黄a大片,九一h片在线免费看,a免费国产一级特黄aa大,国产精品国产主播在线观看,成人精品一区久久久久,一级特黄aa大片,俄罗斯无遮挡一级毛片

分享

android通知欄進(jìn)度條

 軟件團(tuán)隊(duì)頭目 2012-10-25

android通知欄進(jìn)度條

分類: android 2595人閱讀 評(píng)論(3) 收藏 舉報(bào)

項(xiàng)目中需要用到通知欄進(jìn)度條,查找了好多資料、鏈接如下:

  1. Android 為Notification加上一個(gè)進(jìn)度條
  2. Android實(shí)現(xiàn)Service后臺(tái)下載Notification進(jìn)度條
  3. Notification使用詳解之三:通過(guò)服務(wù)更新進(jìn)度通知&在Activity中監(jiān)聽(tīng)服務(wù)進(jìn)度
  4. android service startService bindService
  5. Android中BindService方式使用的理解
  6. [Android 機(jī)制] 大家?guī)兔匆幌逻@個(gè)BUG.
  7. Android 的service的定義可以 作為內(nèi)部類使用嗎?

我先簡(jiǎn)后難寫了兩種實(shí)現(xiàn),第一個(gè)沒(méi)有使用服務(wù),activity轉(zhuǎn)向后臺(tái)進(jìn)度條消失;另一個(gè)使用服務(wù),activity轉(zhuǎn)向后臺(tái)進(jìn)度條仍然繼續(xù)。


以下是第一個(gè)實(shí)現(xiàn):

主界面布局代碼 notify.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas./apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="開(kāi)始" />  
  12.   
  13. </LinearLayout>  
通知欄內(nèi)容布局代碼 notify_content.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas./apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.   
  7.     <ProgressBar  
  8.         android:id="@+id/progressBar1"  
  9.         style="?android:attr/progressBarStyleHorizontal"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentRight="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:layout_marginTop="14dp"  
  15.         android:layout_toRightOf="@+id/imageView1" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/textView1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentRight="true"  
  22.         android:layout_below="@+id/progressBar1"  
  23.         android:layout_marginRight="26dp"/>  
  24.   
  25.     <ImageView  
  26.         android:id="@+id/imageView1"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignBottom="@+id/textView1"  
  30.         android:layout_alignParentLeft="true"  
  31.         android:src="@drawable/ic_launcher" />  
  32.   
  33. </RelativeLayout>  
AndroidManifest.xml 代碼添加如下:

  1. <activity  
  2.     android:label="@string/app_name"  
  3.     android:name=".notify.NotificationActivity1" >  
  4.     <intent-filter >  
  5.         <action android:name="android.intent.action.MAIN" />  
  6.   
  7.         <category android:name="android.intent.category.LAUNCHER" />  
  8.     </intent-filter>  
  9. </activity>  
主程序NotificationActivity1.java代碼如下:

  1. package com.zhang.new_test.notify;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.RemoteViews;  
  15. import android.widget.Toast;  
  16.   
  17. import com.zhang.new_test.R;  
  18.   
  19. public class NotificationActivity1 extends Activity implements OnClickListener {  
  20.   
  21.     private static final int NOTIFICATION_ID = 0x12;  
  22.     private Notification notification = null;  
  23.     private NotificationManager manager = null;  
  24.     private int _progress = 0;  
  25.     private boolean isStop = false;  
  26.   
  27.     @Override  
  28.     protected void onPause() {  
  29.         isStop = false;  
  30.         manager.cancel(NOTIFICATION_ID);  
  31.   
  32.         super.onPause();  
  33.     }  
  34.   
  35.     @Override  
  36.     public void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.notify);  
  39.   
  40.         Button btn = (Button) findViewById(R.id.button1);  
  41.         btn.setOnClickListener(this);  
  42.         notification = new Notification(R.drawable.sg, "帶進(jìn)條的提醒", System.currentTimeMillis());  
  43.   
  44.         notification.contentView = new RemoteViews(getApplication().getPackageName(), R.layout.notify_content);  
  45.         notification.contentView.setProgressBar(R.id.progressBar1, 1000false);  
  46.         notification.contentView.setTextViewText(R.id.textView1, "進(jìn)度" + _progress + "%");  
  47.           
  48.         notification.contentIntent = PendingIntent.getActivity(this0,new Intent(this, NotificationActivity1.class), 0);  
  49.         
  50.         manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  51.   
  52.     }  
  53.       
  54.     @Override  
  55.     public void onClick(View v) {  
  56.         isStop = true;  
  57.         manager.notify(NOTIFICATION_ID, notification);  
  58.         new ProgressThread().start();  
  59.     }  
  60.       
  61.     private class ProgressThread extends Thread {  
  62.   
  63.         @Override  
  64.         public void run() {  
  65.             while (isStop) {  
  66.                 _progress += 10;  
  67.                 Message msg = handler.obtainMessage();  
  68.                 msg.arg1 = _progress;  
  69.                 msg.sendToTarget();  
  70.   
  71.                 try {  
  72.                     Thread.sleep(500);  
  73.                 } catch (InterruptedException e) {  
  74.                     e.printStackTrace();  
  75.                 }  
  76.             }  
  77.         }  
  78.     }  
  79.       
  80.     public Handler handler = new Handler() {  
  81.         @Override  
  82.         public void handleMessage(Message msg) {  
  83.             notification.contentView.setProgressBar(R.id.progressBar1, 100, msg.arg1, false);  
  84.             notification.contentView.setTextViewText(R.id.textView1, "進(jìn)度" + msg.arg1 + "%");  
  85.             manager.notify(NOTIFICATION_ID, notification);  
  86.   
  87.             if (msg.arg1 == 100) {  
  88.                 _progress = 0;  
  89.                 manager.cancel(NOTIFICATION_ID);  
  90.                 isStop = false;  
  91.                 Toast.makeText(NotificationActivity1.this"下載完畢"1000).show();  
  92.             }  
  93.         }  
  94.     };  
  95. }  

以下是第二個(gè)實(shí)現(xiàn):

主界面布局代碼 notify.xml和通知欄內(nèi)容布局代碼 notify_content.xml同上一個(gè)實(shí)現(xiàn)

AndroidManifest.xml 代碼添加如下:

  1. <activity  
  2.     android:label="@string/app_name"  
  3.     android:name=".notify.NotificationActivity2" >  
  4.     <intent-filter >  
  5.         <action android:name="android.intent.action.MAIN" />  
  6.   
  7.         <category android:name="android.intent.category.LAUNCHER" />  
  8.     </intent-filter>  
  9. </activity>  
  10.   
  11. <service android:name=".notify.NotificationActivity2$DownLoadService"/>  
主程序NotificationActivity2.java代碼如下:
  1. package com.zhang.new_test.notify;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.app.Service;  
  8. import android.content.ComponentName;  
  9. import android.content.Context;  
  10. import android.content.Intent;  
  11. import android.content.ServiceConnection;  
  12. import android.os.Binder;  
  13. import android.os.Bundle;  
  14. import android.os.Handler;  
  15. import android.os.IBinder;  
  16. import android.os.Message;  
  17. import android.view.View;  
  18. import android.view.View.OnClickListener;  
  19. import android.widget.Button;  
  20. import android.widget.RemoteViews;  
  21. import android.widget.Toast;  
  22.   
  23. import com.zhang.new_test.R;  
  24.   
  25. public class NotificationActivity2 extends Activity implements OnClickListener {  
  26.       
  27.     private DownLoadService downLoadService;  
  28.       
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.notify);  
  33.           
  34.         Button btn = (Button) findViewById(R.id.button1);  
  35.         btn.setOnClickListener(this);  
  36.     }  
  37.       
  38.     @Override  
  39.     protected void onResume() {  
  40.         super.onResume();  
  41.           
  42.         Intent serviceIntent = new Intent(this,DownLoadService.class);  
  43.         startService(serviceIntent);//  
  44.         bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);  
  45.     }  
  46.       
  47.     @Override  
  48.     protected void onPause() {  
  49.         super.onPause();  
  50.           
  51.         unbindService(serviceConnection);  
  52.     }  
  53.       
  54.     @Override  
  55.     public void onClick(View v) {  
  56.         downLoadService.startDownLoad();  
  57.     }  
  58.       
  59.     private ServiceConnection serviceConnection = new ServiceConnection() {  
  60.           
  61.         @Override  
  62.         public void onServiceConnected(ComponentName name, IBinder service){  
  63.             downLoadService = ((DownLoadService.DownLoadServiceBinder) service).getService();  
  64.             Toast.makeText(NotificationActivity2.this"Service Connected.", Toast.LENGTH_LONG).show();  
  65.         }  
  66.           
  67.         @Override  
  68.         public void onServiceDisconnected(ComponentName name) {  
  69.         }  
  70.     };  
  71.       
  72.     public static class DownLoadService extends Service {  
  73.   
  74.         private static final int NOTIFICATION_ID = 0x12;  
  75.         private Notification notification = null;  
  76.         private NotificationManager manager = null;  
  77.         private int _progress = 0;  
  78.         private boolean isStop = false;  
  79.           
  80.         private Binder serviceBinder = new DownLoadServiceBinder();  
  81.   
  82.         @Override  
  83.         public void onCreate() {  
  84.             super.onCreate();  
  85.               
  86.             notification = new Notification(R.drawable.sg, "帶進(jìn)條的提醒", System.currentTimeMillis());  
  87.   
  88.             notification.contentView = new RemoteViews(getApplication().getPackageName(), R.layout.notify_content);  
  89.             notification.contentView.setProgressBar(R.id.progressBar1, 1000false);  
  90.             notification.contentView.setTextViewText(R.id.textView1, "進(jìn)度" + _progress + "%");  
  91.               
  92.             notification.contentIntent = PendingIntent.getActivity(this0,new Intent(this, NotificationActivity1.class), 0);  
  93.             
  94.             manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  95.         }  
  96.           
  97.         @Override  
  98.         public IBinder onBind(Intent intent) {  
  99.             return serviceBinder;  
  100.         }  
  101.   
  102.         @Override  
  103.         public void onDestroy() {  
  104.             isStop = false;  
  105.             manager.cancel(NOTIFICATION_ID);  
  106.               
  107.             super.onDestroy();  
  108.         }  
  109.           
  110.         public void startDownLoad() {  
  111.             isStop = true;  
  112.             manager.notify(NOTIFICATION_ID, notification);  
  113.             new ProgressThread().start();  
  114.         }  
  115.   
  116.         public class DownLoadServiceBinder extends Binder {  
  117.             public DownLoadService getService() {  
  118.                 return DownLoadService.this;  
  119.             }  
  120.         }  
  121.           
  122.         private class ProgressThread extends Thread {  
  123.   
  124.             @Override  
  125.             public void run() {  
  126.                 while (isStop) {  
  127.                     _progress += 10;  
  128.                     Message msg = handler.obtainMessage();  
  129.                     msg.arg1 = _progress;  
  130.                     msg.sendToTarget();  
  131.   
  132.                     try {  
  133.                         Thread.sleep(1000);  
  134.                     } catch (InterruptedException e) {  
  135.                         e.printStackTrace();  
  136.                     }  
  137.                 }  
  138.             }  
  139.         }  
  140.           
  141.         public Handler handler = new Handler() {  
  142.             @Override  
  143.             public void handleMessage(Message msg) {  
  144.                 notification.contentView.setProgressBar(R.id.progressBar1, 100, msg.arg1, false);  
  145.                 notification.contentView.setTextViewText(R.id.textView1, "進(jìn)度" + msg.arg1 + "%");  
  146.                 manager.notify(NOTIFICATION_ID, notification);  
  147.   
  148.                 if (msg.arg1 == 100) {  
  149.                     _progress = 0;  
  150.                     manager.cancel(NOTIFICATION_ID);  
  151.                     isStop = false;  
  152.                     Toast.makeText(DownLoadService.this"下載完畢"1000).show();  
  153.                 }  
  154.             }  
  155.         };  
  156.     }  
  157. }  
效果我就不貼圖了,前面提到的鏈接中有,效果差不多。

接下來(lái)分析一下代碼,第二個(gè)和第一個(gè)最大區(qū)別是引入了service,這樣可以支持后臺(tái)運(yùn)行,所以第二個(gè)更有實(shí)用性。但是剛開(kāi)始寫代碼,不應(yīng)該引入服務(wù),那樣會(huì)帶來(lái)過(guò)多的復(fù)雜度,出bug時(shí)很難調(diào)試,寫完第一個(gè)在重構(gòu)引入服務(wù)就沒(méi)那么麻煩了,大部分代碼可以復(fù)用的。

主要知識(shí)點(diǎn)就是Notification和bindService的使用,我主要遇到兩個(gè)悲劇,和大家分享一下:

  1. 忘了unbindService,拋了一個(gè)不知道的異常,鏈接6解決了
  2. .notify.NotificationActivity2$DownLoadService 我寫成了 .notify.NotificationActivity2.DownLoadService,鏈接7解決了,這個(gè)花了半天時(shí)間,我就不相信這個(gè)會(huì)錯(cuò),主要是因?yàn)閷懘a時(shí)new 靜態(tài)內(nèi)部類時(shí)都是用 . 啊,忘了配置是用的反射,所以應(yīng)該用 $ 啊,主要是以前沒(méi)寫過(guò)靜態(tài)內(nèi)部類的反射,記住教訓(xùn)了。
希望這篇博文對(duì)大家有幫助。

1
0

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多