package com.example.sh3;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;

import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private DownloadService.DownloadBinder downloadBinder;
private boolean isContinue = false;
private boolean isDownload = false;

private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        downloadBinder = (DownloadService.DownloadBinder) service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};

private TextView downloadProgress;

private void updateDownloadProgress(int progress) {
    downloadProgress.setText('下载进度:' + progress + '%');
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button startDownload = findViewById(R.id.start_download);
    Button pauseDownload = findViewById(R.id.pause_download);
    Button cancelDownload = findViewById(R.id.cancel_download);
    downloadProgress = findViewById(R.id.download_progress);

    pauseDownload.setEnabled(true);
    cancelDownload.setEnabled(false);
    startDownload.setOnClickListener(this);
    pauseDownload.setOnClickListener(this);
    cancelDownload.setOnClickListener(this);
    Intent intent = new Intent(this, DownloadService.class);
    startService(intent);//启动服务
    bindService(intent,connection,BIND_AUTO_CREATE);//绑定服务
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
    }
}

@Override
public void onClick(View v) {
    EditText edittext=(EditText) findViewById(R.id.edit);
    String url,site;
    url=edittext.getText().toString();
    // Toast.makeText(this, site, Toast.LENGTH_SHORT).show();
    if(url.equals(''))
        url= 'https://www.imooc.com/mobile/mukewang.apk';
    // editText.setText(url);

    if (downloadBinder == null) {
        return;
    }
    Button startDownload = findViewById(R.id.start_download);
    Button pauseDownload = findViewById(R.id.pause_download);
    Button cancelDownload = findViewById(R.id.cancel_download);
    int id = v.getId();
    if (id == R.id.start_download) {
        downloadBinder.startDownload(url);
        isDownload = true;
        startDownload.setEnabled(false);
        pauseDownload.setEnabled(true);
        cancelDownload.setEnabled(true);
        downloadProgress.setVisibility(View.VISIBLE); // 显示下载进度
    } else if (id == R.id.pause_download) {
        if (isDownload) {
            if (!isContinue) {
                downloadBinder.pauseDownload();
                pauseDownload.setText('继续下载');
                pauseDownload.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                isContinue = true;
            } else {
                downloadBinder.startDownload(url);
                pauseDownload.setText('暂停下载');
                pauseDownload.setBackgroundColor(getResources().getColor(R.color.colorGray));
                isContinue = false;
            }
        } else {
            Toast.makeText(this, '请先点击下载', Toast.LENGTH_SHORT).show();
        }
    } else if (id == R.id.cancel_download) {
        downloadBinder.cancelDownload();
        startDownload.setEnabled(true);
        pauseDownload.setEnabled(true);
        cancelDownload.setEnabled(false);
        pauseDownload.setBackgroundColor(getResources().getColor(R.color.colorGray));
        downloadProgress.setVisibility(View.GONE); // 隐藏下载进度
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case 1:
            if(grantResults.length>0&&grantResults[0]!=PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this,'拒绝权限无法使用程序',Toast.LENGTH_SHORT).show();
                finish();
            }
            break;
        default:
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unbindService(connection);
}

}

Android 下载服务示例:使用 Service 和 Binder 实现文件下载功能

原文地址: https://www.cveoy.top/t/topic/NkA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录