servicebestpractice项目的更新

servicebestpractice项目的更新,第1张

概述packagecom.example.servicebestpractice;importandroid.app.Notification;importandroid.app.NotificationChannel;importandroid.app.NotificationManager;importandroid.app.PendingIntent;importandroid.app.Service;importandroid.content.Intent;import
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就需要在系统的设置里面开启渠道,通知才能正常d出            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();    }}
总结

以上是内存溢出为你收集整理的servicebestpractice项目的更新全部内容,希望文章能够帮你解决servicebestpractice项目的更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1006135.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存