Android获取进程号,线程号,进程ID,线程ID

Android中获取进程号,线程号,进程ID,线程ID相关信息

package com.danny.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "MainThread";
    public static final String TAG1 = "Thread";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取当前进程号
        Log.i(TAG, "onCreate1: "+Process.myPid());
        //获取当前线程号
        Log.i(TAG, "onCreate2: "+Process.myTid());
        //当前调用该进程的用户号
        Log.i(TAG, "onCreate3: "+Process.myUid());
        //当前线程ID
        Log.i(TAG, "onCreate4: "+Thread.currentThread().getId());
        //主线程ID
        Log.i(TAG, "onCreate5: "+getMainLooper().getThread().getId());
        //当前Activity所在栈的ID
        Log.i(TAG, "onCreate6: "+getTaskId());
        //当前调用该应用程序的用户号
        Log.i(TAG, "onCreate7: "+getApplicationInfo().uid);
        //当前调用该应用程序的进程名
        Log.i(TAG, "onCreate8: "+getApplicationInfo().processName);
    }

    public void call(View source) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG1, "onCreate1: "+Process.myPid());
                Log.i(TAG1, "onCreate2: "+Process.myTid());
                Log.i(TAG1, "onCreate3: "+Process.myUid());
                Log.i(TAG1, "onCreate4: "+Thread.currentThread().getId());
                Log.i(TAG1, "onCreate5: "+getMainLooper().getThread().getId());
                Log.i(TAG1, "onCreate6: "+getTaskId());
                Log.i(TAG1, "onCreate7: "+getApplicationInfo().uid);
                Log.i(TAG1, "onCreate8: "+getApplicationInfo().processName);
            }
        }).start();

    }
}

界面文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="call"
        tools:layout_editor_absoluteX="113dp"
        tools:layout_editor_absoluteY="295dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

结果:

2021-12-30 21:56:19.327 31094-31094/? I/MainThread: onCreate1: 31094
2021-12-30 21:56:19.327 31094-31094/? I/MainThread: onCreate2: 31094
2021-12-30 21:56:19.327 31094-31094/? I/MainThread: onCreate3: 10303
2021-12-30 21:56:19.327 31094-31094/? I/MainThread: onCreate4: 2
2021-12-30 21:56:19.327 31094-31094/? I/MainThread: onCreate5: 2
2021-12-30 21:56:19.330 31094-31094/? I/MainThread: onCreate6: 6722
2021-12-30 21:56:19.330 31094-31094/? I/MainThread: onCreate7: 10303
2021-12-30 21:56:19.330 31094-31094/? I/MainThread: onCreate8: com.danny.myapplication
2021-12-30 21:56:39.460 31094-31208/com.danny.myapplication I/Thread: onCreate1: 31094
2021-12-30 21:56:39.460 31094-31208/com.danny.myapplication I/Thread: onCreate2: 31208
2021-12-30 21:56:39.460 31094-31208/com.danny.myapplication I/Thread: onCreate3: 10303
2021-12-30 21:56:39.460 31094-31208/com.danny.myapplication I/Thread: onCreate4: 15630
2021-12-30 21:56:39.460 31094-31208/com.danny.myapplication I/Thread: onCreate5: 2
2021-12-30 21:56:39.461 31094-31208/com.danny.myapplication I/Thread: onCreate6: 6722
2021-12-30 21:56:39.461 31094-31208/com.danny.myapplication I/Thread: onCreate7: 10303
2021-12-30 21:56:39.461 31094-31208/com.danny.myapplication I/Thread: onCreate8: com.danny.myapplication
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值