Coder Social home page Coder Social logo

simpletools's Introduction

SimpleTools 简单工具

两个单独的lib

  • 一键请求权限
  • 大文件断点下载

一键请求权限

    private void permission() {
        HiPermission hiPermission = new HiPermission();
        String[] permissionList = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.INTERNET};
        hiPermission.requestPermission(this, permissionList, new PermissionCallback() {
            @Override
            public void onClose() {
                Log.i(TAG, "onClose: ");
            }

            @Override
            public void onFinish() {
                Log.i(TAG, "onFinish: ");
                // continue work
            }

            @Override
            public void onDeny(String[] permissions) {
                Log.i(TAG, "onDeny: ");
            }

        });
    }

大文件断点下载

    public void download(View view) {
        SimpleDownloader simpleDownloader = new SimpleDownloader();
        String fileName = simpleDownloader.getFileName(downloadUrl);
        String savePath = Environment.getExternalStorageDirectory().toString() + File.separator + fileName;

        simpleDownloader.download(downloadUrl, savePath, new SimpleDownloadListener() {
            int preProgress;

            @Override
            public void onProgress(DownloadInfo task, long sofarBytes) {
                int progress = (int) (sofarBytes * 100 / task.getTotalLength());
                if (preProgress == progress) {
                    return;
                }
                preProgress = progress;
                progressBar.setProgress(progress);
                ((TextView) view).setText(progress + " %");
                Log.i(TAG, "onProgress: " + preProgress);
            }

            @Override
            public void onComplete(DownloadInfo task) {
                Log.i(TAG, "onComplete: ");
            }

            @Override
            public void onStop(DownloadInfo task, Throwable error) {
                Log.i(TAG, "onStop: ");
                if (task.getState() == DownloadInfo.CANCLE) {
                    // TODO: 2018-12-9 do something
                } else if (task.getState() == DownloadInfo.PAUSE) {
                    // TODO: 2018-12-9 do something
                }
            }
        });
    }
  • 提供取消和暂停的方法,在回调里面会有对应的状态.
        simpleDownloader.cancle();
        simpleDownloader.pause();

需要的配置

  • 在项目root gradle
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  • 在module gradle
	dependencies {
	        implementation 'com.github.xyAugust:SimpleTools:v0.28'
	}

注意 可能还需要如下配置

	android {
            ...
            compileOptions {
                sourceCompatibility JavaVersion.VERSION_1_8
                targetCompatibility JavaVersion.VERSION_1_8
            }
    	}

simpletools's People

Contributors

xyaugust avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.