[初学安卓]安卓页面跳转android.content.Intent运用

package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("页面1")
                }
            }
        }
        val intent = Intent(this, Main2Activity::class.java)//注释2
        startActivity(intent)
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        Greeting("Android")
    }
}

核心

val intent = Intent(this, Main2Activity::class.java)//注释2
        startActivity(intent)

package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("页面2")
                }
            }
        }
        val intent = Intent(this, Main2Activity::class.java)//注释2
        startActivity(intent)
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        Greeting("Android")
    }
}

注册:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
安卓 WebView跳转页面时可能会出现android.os.FileUriExposedException异常。这个异常是因为Android的安全机制限制了应用程序直接暴露本地文件的URI给其他应用程序。当我们在WebView中点击一个链接,如果该链接指向的是一个本地文件的URI,就会抛出这个异常。 解决这个问题的方法有两种: 1. 使用FileProvider:可以通过使用FileProvider来解决这个问题。FileProvider是一个特殊的ContentProvider,它可以控制共享文件的权限。我们需要在AndroidManifest.xml文件中注册FileProvider,并为要共享的文件配置相应的访问权限。然后,可以使用FileProvider的getUriForFile()方法将文件URI转换为共享URI,然后通过Intent将共享URI传递给WebView。这样可以绕过安全限制,使WebView能够顺利跳转到文件页面。 2. 在AndroidManifest.xml中添加文件权限:在应用程序的AndroidManifest.xml文件中添加文件访问权限可以解决此异常。我们可以使用如下代码在<application>标签内添加文件权限: <application ... android:requestLegacyExternalStorage="true"> ... </application> 这样可以设置应用程序使用旧版的外部存储沙盒,从而绕过安全限制,解决异常问题。 总结来说,当安卓WebView跳转页面时出现android.os.FileUriExposedException异常,可以使用FileProvider或者在AndroidManifest.xml中添加文件权限的方式解决。这样可以保证WebView可以顺利跳转到文件页面,而不会触发安全限制导致的异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值