基本信息
源码名称:android模拟文件上传
源码大小:15.33M
文件格式:.zip
开发语言:Java
更新时间:2020-12-18
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.example.servicedemo import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.os.Message import android.os.Messenger import android.view.View import android.widget.Button import android.widget.ProgressBar import java.lang.ref.WeakReference class MainActivity2 : AppCompatActivity(), View.OnClickListener { lateinit var intent2: Intent var progressBar: ProgressBar?=null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) progressBar =findViewById(R.id.progressbar) var btn_start_service: Button? = null var btn_stop_service: Button? = null btn_start_service = findViewById(R.id.btn_start_service) btn_stop_service = findViewById(R.id.btn_stop_service) btn_stop_service?.setOnClickListener(this) btn_start_service?.setOnClickListener(this) } override fun onClick(v: View?) { when (v?.id) { R.id.btn_start_service -> { // val intent = Intent() intent.setClass(this, MyIntentService::class.java) intent.action = MyIntentService.DOWNLOAD_ACTION_KEY intent.putExtra(MyIntentService.TEST_AUTHOR_KEY, "Jere") //将 Messenger 传递给 IntentService, 使其传递消息回来,实现客户端与服务端之间进行沟通 intent.putExtra(MyIntentService.TEST_MESSENGER_KEY, Messenger(MessengerHandler(this))) startService(intent) } R.id.btn_stop_service -> { stopService(intent) } } } class MessengerHandler(activity: MainActivity2) : Handler() { var weakReference: WeakReference<MainActivity2> = WeakReference(activity) override fun handleMessage(msg: Message) { super.handleMessage(msg) //msg 为 IntentService 回复的消息,包含 Bundle 等信息。 val bundle: Bundle = msg.data //获取 IntentService 传递过来的 下载进度 参数 val downloadProgressBarValue: Int = bundle.get(MyIntentService.DOWNLOAD_PROGRESS_BAR_VALUE_KEY) as Int val activity: MainActivity2? = weakReference.get() //将下载进度设置成 ProgressBar 的进度,显示出来。 if (activity != null && !activity.isFinishing) { activity.progressBar?.progress=downloadProgressBarValue } } } }