Android启动下载而无需1DM的文件弹出窗口
创始人
2024-10-09 16:01:20
0

要实现Android启动下载而无需1DM的文件弹出窗口,可以使用Android内置的DownloadManager类。以下是一个代码示例:

首先,在AndroidManifest.xml文件中添加以下权限:



然后,在你的Activity中,添加以下代码来处理下载文件的请求:

import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSION_REQUEST_CODE = 1;
    private static final String DOWNLOAD_URL = "https://example.com/file.zip"; // 替换为你要下载的文件URL

    private Button downloadButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        downloadButton = findViewById(R.id.downloadButton);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkPermission()) {
                    startDownload();
                } else {
                    requestPermission();
                }
            }
        });
    }

    private boolean checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            return result == PackageManager.PERMISSION_GRANTED;
        }
        return true;
    }

    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startDownload();
            } else {
                Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void startDownload() {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DOWNLOAD_URL));
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.zip"); // 替换为你想要保存的文件名

        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        if (downloadManager != null) {
            downloadManager.enqueue(request);
        }
    }
}

以上代码中,通过检查WRITE_EXTERNAL_STORAGE权限是否已授权来决定是否启动下载。如果未授权,则会请求授权。一旦权限被授予,就会使用DownloadManager启动下载,下载完成后会显示通知。在startDownload()方法中,你可以根据需要设置下载文件的保存路径和文件名。

请注意,为了使上述代码正常工作,确保替换DOWNLOAD_URL为你要下载的文件的实际URL,并在布局文件中添加一个Button元素,其id为downloadButton。

相关内容

热门资讯

第三分钟辅助!微乐小程序辅助器... 第三分钟辅助!微乐小程序辅助器出租,微乐小程序免费黑科技,教材教程(有挂安装)-哔哩哔哩一、微乐小程...
第3分钟脚本!微乐广西小程序,... 第3分钟脚本!微乐广西小程序,微乐小程序免费黑科技,项目教程(有挂辅助)-哔哩哔哩微乐小程序免费黑科...
八分钟安装!微信微乐小程序辅助... 八分钟安装!微信微乐小程序辅助软件,微乐小程序免费黑科技,技法教程(有挂app)-哔哩哔哩微信微乐小...
第九分钟安装!微乐小游戏万能修... 第九分钟安装!微乐小游戏万能修改器,微乐小程序免费黑科技,举措教程(有挂工具)-哔哩哔哩1、微乐小游...
六分钟辅助!小程序微乐辅助器i... 六分钟辅助!小程序微乐辅助器ios,微乐小程序黑科技,举措教程(有挂修改器)-哔哩哔哩小程序微乐辅助...
第7分钟插件!微信小程序微乐辅... 第7分钟插件!微信小程序微乐辅助器贴吧,微乐小程序黑科技,法子教程(有挂脚本)-哔哩哔哩1、游戏颠覆...
9分钟脚本!微信小程序微乐挂如... 9分钟脚本!微信小程序微乐挂如何获取,微乐小程序黑科技,指南书教程(有挂软件)-哔哩哔哩1、9分钟脚...
第九分钟脚本!如何下载微乐自建... 第九分钟脚本!如何下载微乐自建房黑科技入口,微乐小程序免费黑科技,妙招教程(有挂平台)-哔哩哔哩1、...
七分钟脚本!微信小程序微乐辅助... 七分钟脚本!微信小程序微乐辅助,微乐小程序免费黑科技,窍门教程(有挂软件)-哔哩哔哩小薇(辅助器软件...
第3分钟辅助!微乐小程序破解,... 第3分钟辅助!微乐小程序破解,微乐小程序免费黑科技,模块教程(有挂神器)-哔哩哔哩所有人都在同一条线...