首页 > 移动平台 > 详细

Android 线程

时间:2019-03-12 16:25:14      阅读:122      评论:0      收藏:0      [点我收藏+]

1.介绍

技术分享图片

2.线程的使用

(1)启动

技术分享图片

(2)执行

技术分享图片

3.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="50dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            app:srcCompat="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            android:textSize="50dp"
            android:text="北京时间" />
    </LinearLayout>
    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:textSize="50dp"
        android:text="北京时间" />
</LinearLayout>

4.java后台

package com.lucky.test35thread;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0x01){
                String time= (String) msg.obj;
                textView.setText(time);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textView2);
        new Thread(){
            @Override
            public void run() {
                while (true){
                    //设置时间显示格式
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String timestr=simpleDateFormat.format(System.currentTimeMillis());//System.currentTimeMillis()获取当前的时间
                    Message message=new Message();//实例化message
                    message.what=0x01;//设置消息发送的验证码
                    message.obj=timestr;//设置消息的内容
                    handler.sendMessage(message); //利用handler发送消息
                    try {
                        Thread.sleep(1000); //让线程延时一秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}

 

Android 线程

原文:https://www.cnblogs.com/luckyplj/p/10517234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!