Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách lấy thông tin Thread Group trong Android

Giới thiệu

Thread là tiến trình nhẹ chạy nền không block UI. Ví dụ này lấy thông tin Thread Group của thread hiện tại.

Bước 1: Tạo project mới

File → New Project trong Android Studio.

Bước 2: Giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="100dp"
    tools:context=".MainActivity">

    <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

Bước 3: MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);
        // Lấy ThreadGroup của thread hiện tại
        textView.setText("Thread hiện tại: " + Thread.currentThread().getThreadGroup());
    }
}

Chạy ứng dụng

Kết quả hiển thị tên thread group (thường là "main").

Cách lấy thông tin Thread Group trong Android