{"id":1569,"date":"2021-06-08T10:07:23","date_gmt":"2021-06-08T02:07:23","guid":{"rendered":"http:\/\/wthee.xyz\/?p=1569"},"modified":"2021-06-08T10:07:23","modified_gmt":"2021-06-08T02:07:23","slug":"jetpack-compose-%e6%9d%83%e9%99%90%e7%94%b3%e8%af%b7","status":"publish","type":"post","link":"https:\/\/wthee.xyz\/index.php\/2021\/06\/08\/jetpack-compose-%e6%9d%83%e9%99%90%e7%94%b3%e8%af%b7\/","title":{"rendered":"Jetpack Compose \u6743\u9650\u7533\u8bf7"},"content":{"rendered":"<blockquote>\n<p>Compose \u7248\u672c\uff1a1.0.0-beta08<\/p>\n<\/blockquote>\n<h4>\u4e3b\u8981\u4ee3\u7801<\/h4>\n<pre><code class=\"\" data-line=\"\">\/**\n * \u6743\u9650\u6821\u9a8c\n *\/\nfun hasPermissions(context: Context, permissions: Array&lt;out String&gt;) = permissions.all {\n    ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED\n}\n\n\/\/\u7533\u8bf7\u6743\u9650\nActivityCompat.requestPermissions(context as Activity, permissions, 1)<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"\" data-line=\"\">\n\/\/\u6743\u9650\nval permissions = arrayOf(\n    Manifest.permission.CAMERA,\n)\nval text = MutableLiveData(&quot;\u70b9\u51fb\u7533\u8bf7\u6743\u9650&quot;)\n\nclass MainActivity : ComponentActivity() {\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContent {\n            PermissionTheme {\n                \/\/ A surface container using the &#039;background&#039; color from the theme\n                Surface(color = MaterialTheme.colors.background,modifier = Modifier.fillMaxSize()) {\n                    PermissionRequestButton()\n                }\n            }\n        }\n    }\n\n    override fun onRequestPermissionsResult(\n        requestCode: Int,\n        permissions: Array&lt;out String&gt;,\n        grantResults: IntArray\n    ) {\n        super.onRequestPermissionsResult(requestCode, permissions, grantResults)\n        if (!hasPermissions(this, permissions)) {\n            text.postValue(&quot;\u672a\u83b7\u53d6\u6743\u9650&quot;)\n\n        } else {\n            text.postValue(&quot;\u6743\u9650\u5df2\u7533\u8bf7&quot;)\n        }\n    }\n}\n\n@Composable\nfun PermissionRequestButton() {\n\n    val context = LocalContext.current\n    \/\/observeAsState \u9700\u6dfb\u52a0 runtime-livedata \u4f9d\u8d56\n    val btnText = text.observeAsState()\n\n    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center){\n        Button(onClick = {\n            if (!hasPermissions(context, permissions)) {\n                \/\/\u7533\u8bf7\u6743\u9650\n                ActivityCompat.requestPermissions(context as Activity, permissions, 1)\n            } else {\n               text.postValue(&quot;\u6743\u9650\u5df2\u7533\u8bf7&quot;)\n            }\n        }) {\n            Text(text = btnText.value?:&quot;&quot;)\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Jetpack Compose \u4e2d\uff0c\u7533\u8bf7\u6743\u9650\u7684\u4e00\u79cd\u65b9\u5f0f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"default","_kad_post_title":"default","_kad_post_layout":"default","_kad_post_sidebar_id":"","_kad_post_content_style":"default","_kad_post_vertical_padding":"default","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_jetpack_newsletter_access":""},"categories":[3,19],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pcwHG0-pj","jetpack-related-posts":[{"id":1579,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/09\/jetpack-compose-navigation-%e7%9a%84%e4%bd%bf%e7%94%a8\/","url_meta":{"origin":1569,"position":0},"title":"Jetpack Compose Navigation \u7684\u4f7f\u7528","author":"wthee","date":"2021\u5e746\u67089\u65e5","format":false,"excerpt":"Compose \u7248\u672c\uff1a1.0.0-beta08 Navigation \u7248\u672c\uff1a2.4.0-alpha0\u2026","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1576,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/09\/jetpack-compose-%e6%8b%96%e5%8a%a8%e6%95%88%e6%9e%9c\/","url_meta":{"origin":1569,"position":1},"title":"Jetpack Compose \u62d6\u52a8\u6548\u679c","author":"wthee","date":"2021\u5e746\u67089\u65e5","format":false,"excerpt":"\u5229\u7528 Modifier.draggable \u76d1\u542c\u62d6\u52a8\u8ddd\u79bb\uff0c\u5b9e\u73b0\u62d6\u52a8\u6548\u679c","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1571,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/08\/jetpack-compose-%e4%bd%bf%e7%94%a8-coil-%e5%8a%a0%e8%bd%bd%e5%9b%be%e7%89%87\/","url_meta":{"origin":1569,"position":2},"title":"Jetpack Compose \u4f7f\u7528 Coil \u52a0\u8f7d\u56fe\u7247","author":"wthee","date":"2021\u5e746\u67088\u65e5","format":false,"excerpt":"Accompanist \u63d0\u4f9b\u4e86\u6613\u4e8e\u4f7f\u7528\u7684 Painter\uff0c\u5b83\u53ef\u4ee5\u4f7f\u7528 Coil \u56fe\u50cf\u52a0\u8f7d\u5e93\u4ece\u5916\u90e8\u6765\u6e90\u2026","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1563,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/08\/jetpack-compose-chipgroup\/","url_meta":{"origin":1569,"position":3},"title":"Jetpack Compose ChipGroup","author":"wthee","date":"2021\u5e746\u67088\u65e5","format":false,"excerpt":"\u5b9e\u73b0 ChipGroup \u7ec4\u4ef6","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":89,"url":"https:\/\/wthee.xyz\/index.php\/2020\/12\/22\/android-navigation-%e6%b7%bb%e5%8a%a0%e8%bf%87%e6%b8%a1%e6%95%88%e6%9e%9c\/","url_meta":{"origin":1569,"position":4},"title":"Android Navigation \u6dfb\u52a0\u8fc7\u6e21\u6548\u679c","author":"wthee","date":"2020\u5e7412\u670822\u65e5","format":false,"excerpt":"\u4f7f\u7528 Container transform\u4e3a\u5bfc\u822a\u7ec4\u4ef6 Navigation \u6dfb\u52a0\u8fc7\u6e21\u52a8\u753b","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1588,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/17\/jetpack-startup\/","url_meta":{"origin":1569,"position":5},"title":"Jetpack Startup","author":"wthee","date":"2021\u5e746\u670817\u65e5","format":false,"excerpt":"startup \u7248\u672c\uff1a1.0.0 Startup App Startup \u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u2026","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1569"}],"collection":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=1569"}],"version-history":[{"count":1,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":1570,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1569\/revisions\/1570"}],"wp:attachment":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}