irakla / MiseAlimi

1 stars 2 forks source link

웹브라우저 로서 안드로이드 앱 #5

Open gwnuysw opened 5 years ago

gwnuysw commented 5 years ago

웹페이지의 화면을 앱에 그대로 보여주는 것을 제가 말한 적이 있는데 잘만하면 그렇게 해도 될 것 같습니다. 그러면 앱을 개발하는데 훨씬더 수월해 질것이라고 생각합니다.

https://developer.android.com/guide/webapps/webview#kotlin

관련 링크입니다. 그러나

private class MyWebViewClient : WebViewClient() {

    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        if (Uri.parse(url).host == "www.example.com") {
            // This is my web site, so do not override; let my WebView load the page
            return false
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
            startActivity(this)
        }
        return true
    }
}

이 부분이 제대로 실행 되지 않습니다. 원인 아는분은 알려주세요

gwnuysw commented 5 years ago

package com.example.myapplication

import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle import android.support.v7.app.AppCompatActivity; import android.view.Menu import android.view.MenuItem

import kotlinx.android.synthetic.main.activity_main.* import android.os.Build import android.support.annotation.RequiresApi import android.support.v4.app.ActivityCompat.startActivityForResult import android.support.v4.content.ContextCompat.startActivity import android.view.KeyEvent import android.webkit.JavascriptInterface import android.webkit.WebView import android.webkit.WebViewClient import android.widget.Toast

class MainActivity : AppCompatActivity() {

@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    val myWebView: WebView = findViewById(R.id.webview)

    myWebView.webViewClient = MyWebViewClient()
    myWebView.settings.javaScriptEnabled = true
    myWebView.addJavascriptInterface(WebAppInterface(this), "Android")

    myWebView.loadUrl("http://172.16.81.181:3000/users/loginpage")

}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.menu_main, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    return when (item.itemId) {
        R.id.action_settings -> true
        else -> super.onOptionsItemSelected(item)
    }
}

// override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { // // Check if the key event was the Back button and if there's history // if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) { // myWebView.goBack() // return true // } // // If it wasn't the Back key or there's no web page history, bubble up to the default // // system behavior (probably exit the activity) // return super.onKeyDown(keyCode, event) // }

} private class MyWebViewClient : WebViewClient() {

override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
    if (url == "http://172.16.81.181:3000/") {
        // This is my web site, so do not override; let my WebView load the page
        return false
    }
    return true
}

} class WebAppInterface(private val mContext: Context) {

/** Show a toast from the web page  */
@JavascriptInterface
fun showToast(toast: String) {
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show()
}

}