歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android基礎教程:下載管理中Notification的使用

Android基礎教程:下載管理中Notification的使用

日期:2017/3/1 10:56:20   编辑:Linux編程
點擊下載按鈕,通知用戶下載任務已添加到下載列表中

布局文件如下:

  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. import java.util.HashMap;
  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.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import com.cloay.down.R;
  13. import com.cloay.down.service.MainService;
  14. import com.cloay.down.utils.Task;
  15. /**
  16. * 模擬下載網頁上的下載按鈕
  17. * DownloadActivity.java
  18. * @author cloay
  19. * 2011-11-18
  20. */
  21. public class DownloadActivity extends Activity {
  22. private Button download;
  23. private Button newdownload;
  24. private NotificationManager notificationManager;
  25. private PendingIntent pendingIntent;
  26. @Override
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. Intent intent = new Intent("com.cloay.down.service.MainService");
  31. this.startService(intent);
  32. notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  33. Intent intent1 = new Intent(this, DownLoadManager.class);
  34. pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_ONE_SHOT);
  35. download = (Button) findViewById(R.id.down);
  36. download.setOnClickListener(new OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. startLoadTask("http://taoke.youkk.net/Mine.mp3");
  40. }
  41. });
  42. newdownload = (Button) findViewById(R.id.newdown);
  43. newdownload.setOnClickListener(new OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. startLoadTask("http://mp3.jayie.com/38.mp3");
  47. }
  48. });
  49. }
  50. private void startLoadTask(String url) {
  51. //使用service後台下載
  52. HashMap<String, String> parm = new HashMap<String, String>();
  53. parm.put("url", url);
  54. Task task = new Task(Task.LOAD_FILE, parm);
  55. MainService.newTask(task);
  56. Notification notification = new Notification();
  57. notification.icon = R.drawable.download_notification;
  58. notification.tickerText = "下載任務已添加到下載列表中,點擊查看!";
  59. notification.defaults = Notification.DEFAULT_SOUND;
  60. notification.setLatestEventInfo(DownloadActivity.this, "下載任務已啟動", "下載任務已添加到下載列表中!", pendingIntent);
  61. notificationManager.notify(0, notification);
  62. }
  63. }
Copyright © Linux教程網 All Rights Reserved