-1

我创建Service了下载电影并通知进度,当我开始下载然后从应用程序退出时我发现了一个问题,我的服务创建新而不破坏前一个。

日志 :

 CREATE  // I press Download button Service Created
 START  
 RUN
 CREATE  // I exit from app and it creates new, without DESTROY
 START
 RUN    
 START   // I press to stop downloading
 DESTROY

下载管理器.java

public class DownloadManager extends Service{



private ExecutorService exec;

private int mb = 1024*1024;
private int Notifid;
private int progressPercent;

private String title;
private String url;

private boolean serviceWork = true;

private NotificationManager manager;
private NotificationCompat.Builder builder;

@Override
public void onCreate() {
    super.onCreate();   
    exec = Executors.newFixedThreadPool(1);
    builder = new NotificationCompat.Builder(getApplicationContext());
    manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    url = intent.getStringExtra(C.SERVICE_URL);
    title = intent.getStringExtra(C.SERVICE_TITLE);

    if(url.equals("cancel")){
     stopSelf();
    }       
    else {          
        Run run = new Run(url, title);
        serviceWork = true;
        exec.execute(run);
    }

    Notifid = 0x45;

    return START_REDELIVER_INTENT;
}

@Override
public void onDestroy() {
    if(url.equals("cancel")){
        cancel();
    }           
    serviceWork = false;
    super.onDestroy();
}


void generateNotify(String msg1, String msg2){              
    builder.setAutoCancel(false);
    builder.setOngoing(true);
    builder.setContentTitle(msg1);
    builder.setContentText(msg2);
    builder.setSmallIcon(R.drawable.ic_launcher, 0);
    builder.setTicker(msg1);
    builder.setProgress(0, 0, true);

    Intent intent = new Intent(this, DownloadManager.class);
    intent.putExtra(C.SERVICE_URL, "cancel");
    PendingIntent pending = PendingIntent.getService(this, 0, intent, 0);
    builder.setContentIntent(pending);
    manager.notify(Notifid, builder.build());   

}

void progress(final int progress, final String msg){
    new Thread(
            new Runnable() {
                @Override
                public void run() {
                    builder.setProgress(100, progress, false);
                    builder.setContentText(msg);
                    manager.notify(Notifid, builder.build());   
                }
         }
    ).start();
}

void ticker(String msg1){
    builder.setTicker(msg1);
    builder.setContentText(msg1);
    manager.notify(Notifid, builder.build());   
}

void cancel(){
    manager.cancel(Notifid);
}

void cancable(){
    builder.setOngoing(false);

    Intent intent  = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("sdcard/Mover/"+title+".mp4"), "video/*");

    PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
    builder.setContentIntent(pending);
    builder.setAutoCancel(true);

    manager.notify(Notifid, builder.build());
}

class Run implements Runnable{

    private String url;
    private String title;

    private int count;
    private int fileLength;

    public Run(String url, String title) {
        this.url = url;
        this.title = title;
    }

    @Override
    public void run() {

        try{

            // Создаем подключение к ссылке.

            URL openUrl = new URL(url);
            URLConnection connection = openUrl.openConnection();
            connection.connect();

            // Проверяем наличие папки если отсуствует создаем.

            File file = new File("sdcard/Mover/");
            file.mkdirs();

            // Размер файла

            fileLength = connection.getContentLength();

            // Загружаем конетнт

            InputStream ips = new BufferedInputStream(openUrl.openStream());
            OutputStream ops = new FileOutputStream("sdcard/Mover/"+title+".mp4");

            // Показываем уведомление
            DownloadManager.this.generateNotify(title, "Всего: " +format(fileLength));

            byte[] data = new byte[1024];
            int total = 0;
            int last = 0;
            int progress = 1;
            int lasttotal = 0;

            int speed = 0;

            long current = System.currentTimeMillis();
            // Читаем
            while ((count = ips.read(data)) != -1) {
                if(serviceWork){
                    ops.write(data, 0, count);                  
                    total += count;    

                    long now = System.currentTimeMillis();

                    // Определяем скорость загрзки. 
                    // Для этого делаем  проверку на каждую секунду
                    if(now > current + 1000){
                        current = now;

                        speed = (total - lasttotal)/1024;                       
                        lasttotal = total;
                    }


                    progressPercent = (total*100)/fileLength;
                    if(last != progressPercent){
                        last = progressPercent;
                        progress++;
                        DownloadManager.this.progress(progress, "Всего: " +format(fileLength) + " / " + format(total) + " / " + speed + "KB/s");                            
                    }
                }
            }

            ops.flush();
            // Закрываем 

            ops.close();
            ips.close();

        }
        catch(Exception e){
            e.printStackTrace();
        }

        finish();           
    }

    void finish(){
        DownloadManager.this.ticker("Загрузка успешно завершена.");
        DownloadManager.this.stopSelf();
        DownloadManager.this.cancable();
    }

    void stop(){
        DownloadManager.this.stopSelf();
        DownloadManager.this.cancel();
    }

}


public String format(int m){

    String size = m%mb+"";
    size = size.substring(0, Math.min(size.length(), 2));

    return m/mb + "." + size + "мб";
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

4

2 回答 2

2

我可以建议你使用 IntentService。android 负责处理您的工作线程逻辑。只需很少的移植工作需要。您可以在下面找到代码

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics

于 2013-02-22T11:05:51.600 回答
1

通过检查您指定的返回标志,onStartCommand我可以看到您已指定START_REDELIVER_INTENT. 此类返回标志状态的 Javadoc;

如果该服务的进程在启动时被杀死(在从 onStartCommand(Intent, int, int) 返回之后),那么它将被安排重新启动,并通过 onStartCommand(Intent, int,诠释)

从这个解释很明显你的服务被杀死了。其原因尚不清楚,但确保服务(及其子线程)保持“活跃”是我之前努力解决的问题。在我们的应用程序中,我们发现手机休眠尤其具有破坏性。我们通过为服务提供唤醒锁定和 WiFi 锁定来“破解”(?)这些细微差别,以确保 (a) 服务在睡眠时不会被封存,并且 (b) 系统保持 wifi 锁定(即没有断开活动的 wifi 连接),而我们在子线程中有工作要做。请在我们对此进行排序后查看您的服务在睡眠环境下的行为。尽管如此,这个标志似乎解释了您的服务明显重新启动和重新尝试下载。

通过简要查看您的代码,我看到您正在从您的线程与您的服务进行通信。IntentService正如 Ranjith 指出的那样,可能适用于这种情况。但是,子线程无法与分离出工作线程的服务通信。原因是托管服务在关闭工作线程后几乎立即被终止,因为服务的工作已经完成,所有剩余的工作都是异步完成的。这可以解释为什么这对您不起作用。

为此,我将尝试以下方法之一;

  1. 使用IntentService并将所有通知代码向下移动到线程中并切断所有 AsyncTask-Service 通信。另请注意我对网络通信的 WakeLocks 和 WiFiLocks 所做的评论。如果不小心使用,这些工​​具对电池来说似乎是相当危险的。
  2. 如果您希望您的服务与您的子线程一样存在,那么我建议START_STICKY从您的服务中返回标志并允许您的应用程序绑定到它(参考:绑定服务以查看它是如何进行的。
于 2013-02-22T11:59:03.290 回答