[컴][안드로이드] AsyncTask 에서 Notification 사용하기

AsyncTask 를 이용한 download notification, progress bar,
Donwloader.java

package com.example.com.mugallery;

import java.net.URL;
import java.util.List;

import android.app.NotificationManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;




public class Downloader extends AsyncTask<URL, Integer, String> {
  
  private NotificationManager mNotificationManager;
  private NotificationCompat.Builder mNotifyBuilder;
  private int mNotifyID = 23; 
  
  private Context mContext;
  
  
  private int mIncr=0;

  //--------------------------------------------------------
  public Downloader(Context context,
            String fileUrl){
    this.mContext = context;
    this.mFileUrl = fileUrl;
    this.mNotifyBuilder = new NotificationCompat.Builder(context)
      .setSmallIcon(R.drawable.gallery_photo_1)
      .setContentTitle("Download")
      .setContentText("Downloading!");
    
    this.mNotificationManager =
        (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    return;
  }
  
  
  
  //--------------------------------------------------------
  @Override
  protected String doInBackground(URL... params) {
  
    int max =10;
    // connect to URL and downloading
    for(;mIncr <= max; mIncr++){
      
      Log.d("namh", "onProgressUpdate mIncr = " + mIncr);
      publishProgress(mIncr, max);
    }

    return "finish downloading";
  }
  
  
  //--------------------------------------------------------
  /**
   * @param its type must be same as the return type of 
   *    doInBackground()
   */
  @Override
  protected void onPostExecute(String messages) {
    
    mNotifyBuilder.setContentText(messages)
     // Removes the progress bar
          .setProgress(0,0,false);

    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
        mNotifyID,
        mNotifyBuilder.build());
  }
  
  //--------------------------------------------------------
  @Override
  protected void onPreExecute() {
    /**
     * to show UI first, before the data is loaded
     */
  
      
  }
  
  //--------------------------------------------------------
  protected void onProgressUpdate(Integer... values) {
    Log.d("namh", "onProgressUpdate values[0] = " + values[0]);
    Log.d("namh", "onProgressUpdate values[1] = " + values[1]);
    int incr = mIncr;
    mNotifyBuilder.setProgress(values[1], values[0], false);
    // Displays the progress bar for the first time.
    mNotificationManager.notify(mNotifyID, mNotifyBuilder.build());
    // Sleeps the thread, simulating an operation
    // that takes time
    try {
      // Sleep for 5 seconds
      Thread.sleep(5*1000);
    } catch (InterruptedException e) {
      Log.d("namh", "sleep failure");
    }
  }
  
  
}
  
  
  

DownloaderTestActivity.java
package com.example.com.mugallery;


import java.net.MalformedURLException;
import java.net.URL;

import android.os.Bundle;
import android.support.v4.app.NotificationCompat;



import android.app.Activity;
import android.app.NotificationManager;




public class DownloaderTestActivity extends Activity {

  public final static String TAG = "Demo";


  

  /**
   * 
   * 
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    Downloader d= new Downloader(this.getApplicationContext(), "http://goo.gl/gTa2s");
    try {
      d.execute(new URL("http://goo.gl/gTa2s"));
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    
    
  }

  
  
  
  
}

댓글 없음:

댓글 쓰기