Android基础教程:下载管理中Notification的使用


点击下载按钮,通知用户下载任务已添加到下载列表中

布局文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:background="@drawable/page"  
  5.     android:layout_height="wrap_content" android:orientation="vertical">  
  6.     <Button android:id="@+id/down"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="下载文件"/>  
  10.     <Button android:id="@+id/newdown"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="新建下载任务"/>  
  14. </LinearLayout>  
主程序代码:
  1. package com.cloay.down.activity;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. import android.app.Activity;  
  6. import android.app.Notification;  
  7. import android.app.NotificationManager;  
  8. import android.app.PendingIntent;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14.   
  15. import com.cloay.down.R;  
  16. import com.cloay.down.service.MainService;  
  17. import com.cloay.down.utils.Task;  
  18. /** 
  19.  * 模拟下载网页上的下载按钮 
  20.  * DownloadActivity.java 
  21.  * @author cloay 
  22.  * 2011-11-18 
  23.  */  
  24. public class DownloadActivity extends Activity {  
  25.     private Button download;   
  26.     private Button newdownload;  
  27.     private NotificationManager notificationManager;  
  28.     private PendingIntent pendingIntent;  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.           
  34.         Intent intent = new Intent("com.cloay.down.service.MainService");  
  35.         this.startService(intent);  
  36.           
  37.         notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  38.         Intent intent1 = new Intent(this, DownLoadManager.class);  
  39.         pendingIntent = PendingIntent.getActivity(this0, intent1, PendingIntent.FLAG_ONE_SHOT);  
  40.           
  41.           
  42.           
  43.         download = (Button) findViewById(R.id.down);  
  44.         download.setOnClickListener(new OnClickListener() {  
  45.               
  46.             @Override  
  47.             public void onClick(View v) {  
  48.                   
  49.                 startLoadTask("http://taoke.youkk.net/Mine.mp3");  
  50.             }  
  51.         });  
  52.         newdownload = (Button) findViewById(R.id.newdown);  
  53.         newdownload.setOnClickListener(new OnClickListener() {  
  54.               
  55.             @Override  
  56.             public void onClick(View v) {  
  57.                   
  58.                 startLoadTask("http://mp3.jayie.com/38.mp3");  
  59.             }  
  60.         });  
  61.     }  
  62.       
  63.     private void startLoadTask(String url) {  
  64.         //使用service后台下载   
  65.         HashMap<String, String> parm = new HashMap<String, String>();  
  66.         parm.put("url", url);  
  67.         Task task = new Task(Task.LOAD_FILE, parm);  
  68.         MainService.newTask(task);  
  69.         Notification notification = new Notification();  
  70.         notification.icon = R.drawable.download_notification;  
  71.         notification.tickerText = "下载任务已添加到下载列表中,点击查看!";  
  72.         notification.defaults = Notification.DEFAULT_SOUND;  
  73.         notification.setLatestEventInfo(DownloadActivity.this"下载任务已启动""下载任务已添加到下载列表中!", pendingIntent);  
  74.         notificationManager.notify(0, notification);  
  75.     }  
  76. }  

相关内容