Android 如何在状态栏中显示下载

How to show download in statusBar in Android

我想从服务器下载文件,为了下载,我使用这段代码并编写以下代码:

public class MainActivity extends AppCompatActivity {

    Button download;
    TextView downloadCount;
    ProgressBar progressBar;

    Future<File> downloading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Enable global Ion logging
        Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);

        setContentView(R.layout.activity_main);

        download = (Button) findViewById(R.id.download);
        downloadCount = (TextView) findViewById(R.id.download_count);
        progressBar = (ProgressBar) findViewById(R.id.progress);

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard.getAbsolutePath() + "/my app");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        final File file = new File(dir, "filename.zip");

        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (downloading != null && !downloading.isCancelled()) {
                    resetDownload();
                    return;
                }

                download.setText("Cancel");
                // this is a 180MB zip file to test with
                downloading = Ion.with(MainActivity.this)
                        .load("http://cdn.p30download.com/?b=p30dl-software&f=PCWinSoft.1AVMonitor.v1.9.1.50_p30download.com.rar")
                        .progressBar(progressBar)
                        .progressHandler(new ProgressCallback() {
                            @Override
                            public void onProgress(long downloaded, long total) {
                                downloadCount.setText("" + downloaded + " / " + total);
                            }
                        })
                        // write to a file
                        .write(file)
                        // run a callback on completion
                        .setCallback(new FutureCallback<File>() {
                            @Override
                            public void onCompleted(Exception e, File result) {
                                resetDownload();
                                if (e != null) {
                                    Toast.makeText(MainActivity.this, "Error downloading file", Toast.LENGTH_LONG).show();
                                    return;
                                }
                                Toast.makeText(MainActivity.this, "File upload complete", Toast.LENGTH_LONG).show();
                            }
                        });
            }
        });
    }

    void resetDownload() {
        // cancel any pending download
        downloading.cancel();
        downloading = null;

        // reset the ui
        download.setText("Download");
        downloadCount.setText(null);
        progressBar.setProgress(0);
    }
}

我想要在点击下载按钮时在 statusBar 中显示此下载!但是在我的代码中只显示 progressBar 中的下载,但我也想 ProgressBar 中显示下载 progress statusBar!

我怎么办?请帮助我,我真的需要这个。

您可以通过将代码更改为类似以下内容来显示进度通知:

public class MainActivity extends AppCompatActivity {

    Button download;
    TextView downloadCount;
    ProgressBar progressBar;

    // --- Add these variables
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    // ---

    Future<File> downloading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Enable global Ion logging
        Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);

        setContentView(R.layout.activity_main);

        download = (Button) findViewById(R.id.download);
        downloadCount = (TextView) findViewById(R.id.download_count);
        progressBar = (ProgressBar) findViewById(R.id.progress);

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard.getAbsolutePath() + "/my app");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        final File file = new File(dir, "filename.zip");

        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (downloading != null && !downloading.isCancelled()) {
                    resetDownload();
                    return;
                }

                download.setText("Cancel");

                // --- Initialize notification manager and the builder
                mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mBuilder = new NotificationCompat.Builder(this);
                mBuilder.setContentTitle("Picture Download")
                    .setContentText("Download in progress")
                    .setSmallIcon(R.drawable.ic_launcher);
                // ---

                // this is a 180MB zip file to test with
                downloading = Ion.with(MainActivity.this)
                    .load("http://cdn.p30download.com/?b=p30dl-software&f=PCWinSoft.1AVMonitor.v1.9.1.50_p30download.com.rar")
                    .progressBar(progressBar)
                    .progressHandler(new ProgressCallback() {
                        @Override
                        public void onProgress(long downloaded, long total) {
                            downloadCount.setText("" + downloaded + " / " + total);

                            // --- Update the progress on the notification
                            mBuilder.setProgress(100, incr, false);
                            mNotifyManager.notify(0, mBuilder.build());
                            // ---
                        }
                    })
                    // write to a file
                    .write(file)
                    // run a callback on completion
                    .setCallback(new FutureCallback<File>() {
                        @Override
                        public void onCompleted(Exception e, File result) {
                            resetDownload();
                            if (e != null) {
                                Toast.makeText(MainActivity.this, "Error downloading file", Toast.LENGTH_LONG).show();
                                return;
                            }
                            Toast.makeText(MainActivity.this, "File upload complete", Toast.LENGTH_LONG).show();
                        }
                    });
                        });
            }
        });
    }

    void resetDownload() {
        // cancel any pending download
        downloading.cancel();
        downloading = null;

        mBuilder.setContentText("Download complete")
        // Removes the progress bar
                .setProgress(0, 0, false);
        mNotifyManager.notify(ID, mBuilder.build());

        // reset the ui
        download.setText("Download");
        downloadCount.setText(null);
        progressBar.setProgress(0);
    }
}

阅读此处了解有关在通知中显示进度的信息:Displaying Progress in a Notification