Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

android - How to call this function from other activities?

i need help in this kotlin code pls ... i have checkConnctivvity() function and it's working perfectly ...

fun checkConnectivity(){

    val cm=getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetwork =cm.activeNetworkInfo
    val isConnected=activeNetwork != null && activeNetwork.isConnectedOrConnecting}

1/ i tried different ways to call it from other activities but i couldn't ... How to solve it plz

2/ i want to add the checkConnectivity() function in webChromeClient , so each time the progressBarChanged it will verify the connection .. but didn't worked

class ExodyaActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_exodya)

    //Problem1: failed to call the checkConnectivity() from MainActivity

    val myWebview   = ExoWeb
    var ProgressBar = progressBar
    var FrameLayout = frameLayout
    var TextView  = loadPrs

    myWebview.visibility = View.GONE
    frameLayout.visibility = View.GONE
    loadLinear.visibility = View.GONE

    //webview
    myWebview.webViewClient= WebViewClient()
    myWebview.settings.javaScriptEnabled=true
    myWebview.loadUrl("url")

    ProgressBar.max = 100

    myWebview.webChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView, progress: Int) {

            // Problem2: checkConnectivity()  didn't work here !!!!

            progressBar.progress = progress
            if(progress < 90) {
                loadPrs.text = " %" + progress
                myWebview.visibility = View.GONE
                frameLayout.visibility = View.VISIBLE
                loadLinear.visibility = View.VISIBLE
            }
            if (progress == 100) {

                FrameLayout.visibility = View.GONE
                loadLinear.visibility = View.GONE
                myWebview.visibility = View.VISIBLE
            }
        }
    }

}
// onBackPressed (back in webview history)
 override fun onBackPressed() {
     var myWebview   = ExoWeb
    if (myWebview.canGoBack()) {
        myWebview.goBack()
    } else {
         super.onBackPressed()
            Toast.makeText(this,"See You Next Time!",Toast.LENGTH_SHORT).show()
             }
        }

}

Thanks in advance :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Create Kotlin file, e.g. named Utils;
  2. Move function to that file, and add Context parameter:

    fun checkConnectivity(ctx: Context): Boolean {
        val cm = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetwork =cm.activeNetworkInfo
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting
    } 
    

    If you intend to use it only in Activity you can create extension function without Context parameter:

    fun Activity.checkConnectivity(): Boolean {
        val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetwork =cm.activeNetworkInfo
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting
    }
    
  3. Call that function from wherever you want. If you call it from Activity just use code:

    checkConnectivity(this@YourActivity)
    

    If you created extension function, just call it in Activity without passing any parameters:

    checkConnectivity()
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...