Coder Social home page Coder Social logo

activityresultlauncher's Introduction

ActivityResultLauncher

基于官方api,开发的代替startActivityForResult和requestPermission,这两个方法是常用的,官方还提供了其他的api,可根据需要自行继承BaseActivityResultLauncher

在根目录的build.gradle添加如下代码:

allprojects {
   repositories {
       // ...
       maven { url 'https://www.jitpack.io' }
   }
}

在工程目录的build.gradle添加如下代码:

dependencies {
   implementation 'com.github.op123355569:ActivityResultLauncher:lastVersion'
}

Java用法:

// StartActivityForResult
ProxyStartActivityForResult startActivityForResult1 = new ProxyStartActivityForResult(this);
startActivityForResult1.launch(new Intent(JavaActivity.this, ResultActivity.class), result -> {
    if (result != null) {
        int resultCode = result.getResultCode();
        String name = "";
        int age = 0;

        if (result.getData() != null) {
            name = result.getData().getStringExtra("name");
            age = result.getData().getIntExtra("age", age);
        }

        Log.e("Java", "ResultCode:" + resultCode + ",name:" + name + ",age:" + age);
    }
});
           
// RequestPermission
ProxyRequestPermission requestPermission1 = new ProxyRequestPermission(this);
requestPermission1.launch(
    (result, isAgreeAll) -> Log.e("Java", "权限是否全部同意:" + isAgreeAll),
    Manifest.permission.CAMERA
));

Kotlin用法:

// StartActivityForResult
val startActivityForResult1 = ProxyStartActivityForResult(this)
startActivityForResult1.launch(Intent(this, ResultActivity::class.java), object : OnActivityResultListener {
    override fun onActivityResult(result: ActivityResult?) {
        val resultCode = result?.resultCode
        val name = result?.data?.getStringExtra("name")
        val age = result?.data?.getIntExtra("age", 0)
        Log.e("Kotlin", "ResultCode:$resultCode,name:$name,age:$age")
    }
})

// RequestPermission
requestPermission1 = ProxyRequestPermission(this)
requestPermission1.launch(
    object : OnRequestPermissionsResultListener {
        override fun onRequestPermissionsResult(result: Map<String, Boolean>, isAgreeAll: Boolean) {
            Log.e("Kotlin", "权限是否全部同意:$isAgreeAll")
        }
    },
    Manifest.permission.CAMERA
)

具体用法Demo中有写。

activityresultlauncher's People

Contributors

op123355569 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.