zoukankan      html  css  js  c++  java
  • servicebestpractice项目的更新

    package com.example.servicebestpractice;
    
    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.os.Binder;
    import android.os.Build;
    import android.os.Environment;
    import android.os.IBinder;
    import android.webkit.DownloadListener;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.io.File;
    
    import androidx.annotation.Nullable;
    import androidx.core.app.NotificationCompat;
    
    public class DownloadService extends Service {
        private DownloadTask downloadTask;
        private String downloadUrl;
        private IDownloadListener listener = new IDownloadListener() {
            @Override
            public void onProgress(int progress) {
                getNotificationManager().notify(1, getNotification("Downloading...", progress));
            }
    
            @Override
            public void onSuccess() {
                downloadTask = null;
                //下载成功时将前台服务关闭,并创建一个下载成功的通知
                stopForeground(true);
                getNotificationManager().notify(1, getNotification("Download Success", -1));
                Toast.makeText(DownloadService.this, "Download Success", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onFailed() {
                downloadTask = null;
                // 下载失败时将前台服务通知关闭,并创建一个下载失败的通知
                downloadTask = null;
                stopForeground(true);
                getNotificationManager().notify(1,getNotification("Download Failed",-1));
                Toast.makeText(DownloadService.this,"Download Failed",Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onPaused() {
                downloadTask = null;
                stopForeground(true);
                Toast.makeText(DownloadService.this,"Paused",Toast.LENGTH_SHORT).show();
    
            }
    
            @Override
            public void onCanceled() {
                downloadTask = null;
                stopForeground(true);
                Toast.makeText(DownloadService.this,"Canceled",Toast.LENGTH_SHORT).show();
    
            }
        };
    
       private DownloadBinder mBinder = new DownloadBinder();
    
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
        public class DownloadBinder extends Binder {
            public void startDownload(String url){
                if(downloadTask==null){
                    downloadUrl = url;
                    downloadTask = new DownloadTask(listener);
                    downloadTask.execute(downloadUrl);
                    startForeground(1,getNotification("Downloading...",0));
                    Toast.makeText(DownloadService.this,"Downloading...",Toast.LENGTH_SHORT).show();
                }
    
            }
            public void pauseDownload(){
                if(downloadTask!=null){
                    downloadTask.pauseDownload();
                }
    
            }
            public void cancelDownload(){
                if(downloadTask!=null){
                    downloadTask.cancelDownload();
                }else{
                    if(downloadUrl!=null){
                        //取消下载时删除文件,并通知关闭
                        String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
                        String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
                        File file = new File(directory+fileName);
                        if(file.exists()){
                            file.delete();
                        }
                        getNotificationManager().cancel(1);
                        stopForeground(true);
                        Toast.makeText(DownloadService.this,"Canceled",Toast.LENGTH_SHORT).show();
                    }
                }
    
            }
    
        }
    
        private NotificationManager getNotificationManager() {
            return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        private Notification getNotification(String title, int progress) {
            Intent intent = new Intent(this, MainActivity.class);
            PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    
            NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    
    
            if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
                //只在Android O之上需要渠道,这里的第一个参数要和下面的channelId一样
                NotificationChannel notificationChannel = new NotificationChannel("download","name",NotificationManager.IMPORTANCE_HIGH);
                //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
                manager.createNotificationChannel(notificationChannel);
            }
    
    
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "download");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher));
            builder.setContentIntent(pi);
            builder.setContentTitle(title);
            if (progress > 0) {
                //大于0开始显示进度
                builder.setContentText(progress + "%");
                builder.setProgress(100, progress, false);
            }
            return builder.build();
        }
    
    
    }
    
  • 相关阅读:
    Assembly Manifest 通俗简易手册
    CruiseControl服务器安装配置
    关于URL编码
    从A到Z来说说Web开发
    通过注册表查看 .NET Framework的版本信息
    云数据存在哪里?
    C#中你可能不知道的8件事(zz)
    用PBKDF2 或BCrypt 来存储密码
    C++编译器什么时候为我们自动生成拷贝构造函数?
    C#中你可能不知道的8件事(zz)
  • 原文地址:https://www.cnblogs.com/c-x-a/p/14788194.html
Copyright © 2011-2022 走看看