即使从最近的任务中清除了应用程序,Android仍然保持下载状态

时间:2018-10-02 10:39:34

标签: android

我正在使用服务来下载文件,但是如果我从最近的应用程序列表中清除了该应用程序,则下载将停止。我尝试将服务作为前台运行,但是没有运气。我该如何实现?即使从最近的任务中删除了应用程序,也要下载队列中的多个文件。

 public class ApkDownloadService extends Service {

        public static final String EXTRA_APP_DETAILS = ApkDownloadService.class.getName().concat("_app_details");
        int NOTIFICATION_ID = ((Number) System.currentTimeMillis()).intValue();
        private String TAG = "ApkDownloadService";
        String appId;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        NotificationManagerCompat notificationManager;
        NotificationCompat.Builder mBuilder;

        // Issue the initial notification with zero progress
        int PROGRESS_MAX = 100;
        int PROGRESS_CURRENT = 0;
        int fileLength;

        @Override
        public void onCreate() {
            super.onCreate();
            notificationManager = NotificationManagerCompat.from(this);
            Log.d(TAG,"onCreate");
        }

        @Override
        public void onDestroy() {
            notificationManager.cancel(NOTIFICATION_ID);
            Log.d(TAG,"onDestroy");
            super.onDestroy();
        }

        @Override
        public void onTaskRemoved(Intent rootIntent) {
            notificationManager.cancel(NOTIFICATION_ID);
            Log.d(TAG,"onTaskRemoved");
            super.onTaskRemoved(rootIntent);
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            Log.d(TAG,"onBind");
            return null;
        }


        @Override
        public boolean onUnbind(Intent intent) {
            Log.d(TAG,"onUnbind");
            return super.onUnbind(intent);

        }

        @SuppressLint("StaticFieldLeak")
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            assert intent != null;
            Result result = intent.getParcelableExtra(EXTRA_APP_DETAILS);
            Log.d(TAG,"onStartCommand");
            appId = result.getApplicationId();

            mBuilder = new NotificationCompat.Builder(this, appId);
            mBuilder.setContentTitle(result.getName())
                    .setContentText("Download in progress")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(NotificationCompat.PRIORITY_LOW);

            mBuilder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);

            startForeground((int) result.getId(), mBuilder.build());


            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... voids) {
                    URL url;
                    try {
                        url = new URL(result.getPackage().getApk());
                        URLConnection conection = url.openConnection();
                        conection.connect();
                        fileLength = conection.getContentLength();
                        long total = 0;
                        int count, tmpPercentage = 0;
                        InputStream input = url.openStream();
                        byte[] chunk = new byte[4096];

                        while ((count = input.read(chunk)) != -1) {
                            total += count;
                            outputStream.write(chunk, 0, count);
                            PROGRESS_CURRENT = (int) ((total * 100) / fileLength);
                            if (PROGRESS_CURRENT > tmpPercentage) {
                                EventBus.getDefault().post(String.valueOf(PROGRESS_CURRENT)); // Posting download percentage
                                mBuilder.setContentText(PROGRESS_CURRENT + "%");
                                mBuilder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);
                                notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
                                tmpPercentage = PROGRESS_CURRENT;
                            }
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);

                }

                @Override
                protected void onCancelled() {
                    super.onCancelled();
                    Log.d(TAG,"onCancelled");
                }
            }.execute();



            return START_REDELIVER_INTENT;
        }
    }

我不确定我的前台服务出了什么问题。如果我从最近的任务中清除了该应用程序,则下载将停止。

0 个答案:

没有答案