Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

Android Service start

i created Service to download movies and notificate progress, and i found a problem when i start downloading and then exit from app my Service create new without destroying the previous one.

Log :

 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

DownloadManager.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;
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can sugget you using IntentService. Where android take cares of your worker thread logic.just little porting work need. You can find the code below

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...