Timer().schedule 在 android studio Kotlin

问题描述

定时器任务在一定延迟时间内没有被调用

这是我在 Kotlin 中的代码

public class getLocationFromApi : AsyncTask<Void?,Void?,Void?>()
{

    private val auth = FirebaseAuth.getInstance()

    private var vehicleAPI_Id: String? = null
    private var vehicleNo: String? = null
    private var imei: String? = null
    private var angle: String? = null
    private var date: String? = null
    private var ignition: String? = null
    private var lat: String? = null
    private var lcoation: String? = null
    private var lon: String? = null
    private var speed: String? = null
    private var tempr: String? = null
    private var sb: String? = null

    private var tpLink=""
    private var API_LINK : String? = "http://ctyf.co.in/api/companyvehiclelatestinfo?token=F934A0C439&vehname="

    override fun onPreExecute() {
        super.onPreExecute()
        getVehicleNoFromFirestoreDB()

    }

    override fun doInBackground(vararg params: Void?): Void? {
        try {
                val httpHandler = HttpHandler()
                val json_dataString: String = httpHandler.
                makeServiceCall(""" http://ctyf.co.in/api/companyvehiclelatestinfo?token=F934A0C439&vehname=$vehicleAPI_Id""")
                val jsonObject = JSONObject(json_dataString)
                val jsonarray = jsonObject.getJSONArray("Vehicle")

                for (i in 0 until jsonarray.length()) {
                    val js = jsonarray.getJSONObject(i)

                    vehicleNo = js.getString("VehicleNo")
                    imei = js.getString("Imei")
                    lcoation = js.getString("Location")
                    date = js.getString("Date")
                    tempr = js.getString("Tempr")
                    ignition = js.getString("Ignition")
                    lat = js.getString("Lat")
                    lon = js.getString("Long")
                    speed = js.getString("Speed")
                    angle = js.getString("Angle")
                    sb = """
                    VehicleNo : $vehicleNo
                    $imei
                    $lcoation
                    $date
                    $tempr
                    $ignition
                    lat : $lat
                    long :$lon
                    speed :$speed
                    angle :$angle
                    """.trimIndent()

                    /*  String email  = js.getString("email");
                    JSONObject  phone=js.getJSONObject("phone");
                    String mobile =phone.getString("mobile");
                    tv.setText(name);*/
                }

        } catch (e: Exception) {
            println(e.message)
        }
        return null
    }

    override fun onPostExecute(aVoid: Void?) {
        super.onPostExecute(aVoid)

        Log.d("api2",sb.toString())

        Log.d("lat",lat.toString())

        if(lat!=null) {

            Timer().schedule(timerTask {
 val geoPoint = GeoPoint(lat!!.todouble(),lon!!.todouble())

                val liveUser = LiveUser(auth.uid.toString(),geoPoint,Timestamp.Now())

                updateLiveLocation(liveUser)

                Log.e("NIlu_TAG","Hello World")

            },1000)
        }



    }

    //save location every 4 seconds
    private fun updateLiveLocation(liveUser : LiveUser)
    {
        try
        {
            val locationRef = FirebaseFirestore.getInstance()
                .collection("LiveLocation")

            locationRef.document(auth.uid.toString())
                .set(liveUser)
                .addOnCompleteListener { task ->
                    if (task.isSuccessful)
                    {
                        Log.i("LiveLocation","updateLiveLocation : Success.")
                    }
                }
        }
        catch (e : NullPointerException) { }
    }

    private fun getVehicleNoFromFirestoreDB() {
        try {
            val db = FirebaseFirestore.getInstance()
            db.collection("OperatorUsers").document(auth.uid.toString()).get().addOnSuccessListener { snapshot ->

                if(snapshot.exists()){
                    val user = snapshot.toObject(User::class.java)
                    if(user!=null){
                        vehicleAPI_Id  = user.vehicleNo
                        if(!vehicleAPI_Id.equals("not added")) {
                            //To attach vehicleId/vehicleNo to the API link for the particular device
                            tpLink = """$API_LINK$vehicleAPI_Id"""
                            Log.d("API_LINK",tpLink)
                        } else {
                            Log.d("vehicleNo","vehicleNo not added")
                        }
                    }

                }else{
                    Log.d("err","no data")
                }
                }
        } catch (e: Exception) { }
    }
}

嗯,这不是活动,我从主片段调用这个类,我想每隔一秒更新一次数据。

我不知道为什么计时器任务在不活动时不起作用......?

我怎样才能做到这一点? 有什么建议么 ???请帮忙....

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)