Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在单独的线程中运行主活动的一部分_Android_Kotlin_Background Process - Fatal编程技术网

Android 在单独的线程中运行主活动的一部分

Android 在单独的线程中运行主活动的一部分,android,kotlin,background-process,Android,Kotlin,Background Process,我正在研究在单独的线程或进程中运行onResume函数的方法,这样它就不会阻止该活动的加载 我正在研究运行我在MainActivity中单独使用的onResume功能的方法。onResume中包含的是一组例程,当应用程序启动或恢复时,这些例程从SharedRef加载数据。然而,onResume进程延迟了整个活动的加载,所以我想的是,它可以从一个单独的进程运行,以便不延迟整个活动的加载吗 package com.taylorworld.tw1 import android.content.Con

我正在研究在单独的线程或进程中运行onResume函数的方法,这样它就不会阻止该活动的加载

我正在研究运行我在MainActivity中单独使用的onResume功能的方法。onResume中包含的是一组例程,当应用程序启动或恢复时,这些例程从SharedRef加载数据。然而,onResume进程延迟了整个活动的加载,所以我想的是,它可以从一个单独的进程运行,以便不延迟整个活动的加载吗

package com.taylorworld.tw1

import android.content.Context
import android.content.Context.*
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.view.MenuItem
import android.support.v4.widget.DrawerLayout
import android.support.design.widget.NavigationView
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.View
import android.widget.Toast
import com.google.firebase.database.FirebaseDatabase
//import com.google.firebase.firestore.FirebaseFirestore
import kotlinx.android.synthetic.main.content_main.*
import java.sql.Types.NULL

class MainActivity : AppCompatActivity(),     NavigationView.OnNavigationItemSelectedListener {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val fab: FloatingActionButton = findViewById(R.id.fab)
    fab.setOnClickListener { view ->
        val intent = Intent(this, Main2Activity::class.java)
        val sharedPref = this?.getPreferences(Context.MODE_PRIVATE)
        val mystr = sharedPref.getInt(getString(R.string.STR), 0)
        intent.putExtra("data", mystr)
        startActivity(intent)
    }
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val toggle = ActionBarDrawerToggle(
        this, drawerLayout, toolbar, R.string.navigation_drawer_open,     R.string.navigation_drawer_close
    )
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()

    navView.setNavigationItemSelectedListener(this)

    //val db = FirebaseFirestore.getInstance()
}

override fun onBackPressed() {
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is     present.
    menuInflater.inflate(R.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 onNavigationItemSelected(item: MenuItem): Boolean {
    // Handle navigation view item clicks here.
    when (item.itemId) {
        R.id.nav_home -> {
            // Handle the camera action
            val i = Intent(Intent.ACTION_VIEW,     Uri.parse("https://www.facebook.com/brobostigon/"))
            startActivity(i)
        }
        R.id.nav_gallery -> {

        }
        R.id.nav_slideshow -> {

        }
        R.id.nav_tools -> {

        }
        R.id.nav_share -> {

        }
        R.id.nav_send -> {

        }
    }
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    drawerLayout.closeDrawer(GravityCompat.START)
    return true
}

public fun saveStr(view: View) {
    var strength = Integer.parseInt(editText2.text.toString())
    // this should be getPreferences
    val sharedPref = this?.getPreferences(Context.MODE_PRIVATE);
    with(sharedPref.edit()) {
        putInt(getString(R.string.STR), strength)
        apply()
    }
    Toast.makeText(this, "Strength Saved", Toast.LENGTH_SHORT).show();

    //val database = FirebaseDatabase.getInstance()
    //val myRef = database.getReference("str")
    //myRef.setValue(strength)
}

public fun saveDex(view: View) {
    var dexterity = Integer.parseInt(editText.text.toString())
    // this should be getPreferences
    val sharedPref = this?.getPreferences(Context.MODE_PRIVATE);
    with(sharedPref.edit()) {
        putInt(getString(R.string.DEX), dexterity)
        apply()
    }
    Toast.makeText(this, "Dexterity Saved",     Toast.LENGTH_SHORT).show();
}

public fun saveInt(view: View) {
    var intelligance = Integer.parseInt(editText4.text.toString())
    // this should be getPreferences
    val sharedPref = this?.getPreferences(Context.MODE_PRIVATE);
    with(sharedPref.edit()) {
        putInt(getString(R.string.INT), intelligance)
        apply()
    }
    Toast.makeText(this, "Intelligence Saved", Toast.LENGTH_SHORT).show();
}

public fun saveWis(view: View) {
    var wisdom = Integer.parseInt(editText3.text.toString())
    // this should be getPreferences
    val sharedPref = this?.getPreferences(Context.MODE_PRIVATE);
    with(sharedPref.edit()) {
        putInt(getString(R.string.WIS), wisdom)
        apply()
    }
    Toast.makeText(this, "Wisdom Saved", Toast.LENGTH_SHORT).show();
}

public fun saveCha(view: View) {
    var charisma = Integer.parseInt(editText5.text.toString())
    // this should be getPreferences
    val sharedPref = this?.getPreferences(Context.MODE_PRIVATE);
    with(sharedPref.edit()) {
        putInt(getString(R.string.CHA), charisma)
        apply()
    }
    Toast.makeText(this, "charisma Saved", Toast.LENGTH_SHORT).show();
}

public fun saveCon(view: View) {
    var constitution = Integer.parseInt(editText6.text.toString())
    // this should be getPreferences
    val sharedPref = this?.getPreferences(Context.MODE_PRIVATE);
    with(sharedPref.edit()) {
        putInt(getString(R.string.CON), constitution)
        apply()
    }
    Toast.makeText(this, "Constitution Saved", Toast.LENGTH_SHORT).show();
}

public fun saveChr(view: View) {
    var chrname = editText7.text.toString()
    // this should be getPreferences
    val sharedPref = this?.getPreferences(Context.MODE_PRIVATE);
    with(sharedPref.edit()) {
        putString(getString(R.string.CHR), chrname)
        apply()
    }
    Toast.makeText(this, "Character Name Saved", Toast.LENGTH_SHORT).show();
}

    override fun onResume() {
        super.onResume()

        //public fun readSP(view: View) {

        val sharedPref = this?.getPreferences(Context.MODE_PRIVATE)

        val mystr = sharedPref.getInt(getString(R.string.STR), 0);
        editText2.setText(Integer.toString(mystr))

        val mydex = sharedPref.getInt(getString(R.string.DEX), 0);
        editText.setText(Integer.toString(mydex))

        val myint = sharedPref.getInt(getString(R.string.INT), 0);
        editText4.setText(Integer.toString(myint))

        val mywis = sharedPref.getInt(getString(R.string.WIS), 0);
        editText3.setText(Integer.toString(mywis))

        val mycon = sharedPref.getInt(getString(R.string.CON), 0);
        editText6.setText(Integer.toString(mycon))

        val mycha = sharedPref.getInt(getString(R.string.CHA), 0);
        editText5.setText(Integer.toString(mycha))

        val mychr = sharedPref.getString(getString(R.string.CHR), null);
        editText7.setText(mychr)
    }
}

您可以使用协程在后台线程中加载数据,然后切换回UI线程来填充视图,如下所示(为了清楚起见,我只使用了2个属性):

但是,请注意,加载首选项的速度非常快,因此我怀疑这是否是您的瓶颈,您可能需要进行一些分析,以查看时间花在哪里


还请注意,您正在混合
进程
线程
-您的整个应用程序在
进程
中运行,并且在该进程中有多个
线程
-您可以在进程内切换线程,但是您的整个应用程序是一个单一的
进程

当您使用kotlin时,您也可以使用Anko库,它允许您在后台使用doAsync{}执行并返回UI线程

// Example from Anko documentation
doAsync {
    // Long background task
    uiThread {
        result.text = "Done"
    }
}

检查它:

啊,好的,这看起来是一个很好的方法,我已经做了笔记,将尝试一下。这是一个很好的观点,我把它们混在一起了,你说的对,呜呜。这就是我所想的,它确实看起来做起来很快,我会做更多的测试。我已经围绕您建议的使用协同程序进行了构建,它工作得很好,:)
// Example from Anko documentation
doAsync {
    // Long background task
    uiThread {
        result.text = "Done"
    }
}