android获取子线程id,Android 开发 知晓各种id信息 获取线程ID、activityID、内核ID

/*** Returns the identifier of this process's user.

* 返回此进程的用户的标识符。*/Log.e(TAG,"Process.myUid() = " +android.os.Process.myTid());/*** Returns the identifier of this process, which can be used with

* killProcess and sendSignal.

* 返回此进程的标识符,可用于进程和发送信号。*/Log.e(TAG,"Process.myPid() = " +android.os.Process.myPid());/*** Returns the identifier of the calling thread, which be used with

* setThreadPriority(int, int).

* 返回调用线程的标识符,该标识符与StTeRead优先级(int,int)。*/Log.e(TAG,"Process.myTid() = " +android.os.Process.myTid());/*** Returns the thread's identifier. The ID is a positive long generated

* on thread creation, is unique to the thread, and doesn't change

* during the lifetime of the thread; the ID may be reused after the

* thread has been terminated.

* 返回线程的标识符。ID是正长生成的关于线程创建,对于线程是唯一的,并且不会改变。

* 在线程的生存期内,ID可以在线程已被终止。*/

//返回当前线程的id

Log.e(TAG, "Thread.currentThread().getId() = "

+Thread.currentThread().getId());//返回主线程的id

Log.e(TAG, "getMainLooper().getThread().getId() = "

+getMainLooper().getThread().getId());//返回当前应用的主线程id

Log.e(TAG,"((getApplication().getMainLooper()).getThread()).getId() = "

+((getApplication().getMainLooper()).getThread())

.getId());/*** Return the identifier of the task this activity is in. This

* identifier will remain the same for the lifetime of the activity.

* 返回此活动正在执行的任务的标识符。这个标识符对于活动的生存期将保持不变。*/

//返回activity任务栈的id

Log.e(TAG, "getTaskId() = " +getTaskId());/*** The kernel user-ID that has been assigned to this application;

* currently this is not a unique ID (multiple applications can have the

* same uid).

* 已分配给该应用程序的内核用户ID;这不是一个唯一的ID(多个应用程序可以有相同的UID)。*/Log.e(TAG,"getApplicationInfo().uid = " +getApplicationInfo().uid);/*** The name of the process this application should run in. From the

* "process" attribute or, if not set, the same as packageName.

* 此应用程序应运行的进程的名称。从“进程”属性,或如果没有设置,与PACKAGEName相同。*/Log.e(TAG,"getApplicationInfo().processName = "

+getApplicationInfo().processName);/*** 得到当前activity的信息*/Log.e(TAG,"Activity.toString:"+this.toString());new Thread(newRunnable() {

@Overridepublic voidrun() {//返回当前线程的id//TODO Auto-generated method stub

Log.e(TAG, "Thread.currentThread().getId() = "

+Thread.currentThread().getId());

}

}).start();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在非 `Activity` 类中获取 `Activity` 的 ID 并使用,可以通过以下步骤实现: 1. 定义一个接口,用于将 `Activity` 的 ID 传递给非 `Activity` 类。例如: ```java public interface OnActivityIdSetListener { void onActivityIdSet(int activityId); } ``` 2. 在 `Activity` 中实现该接口,并将 `Activity` 的 ID 传递给非 `Activity` 类。例如: ```java public class MainActivity extends AppCompatActivity implements OnActivityIdSetListener { private int mActivityId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 ActivityID mActivityId = this.hashCode(); // 传递 ActivityID 给非 Activity 类 NonActivityClass nonActivityClass = new NonActivityClass(); nonActivityClass.setOnActivityIdSetListener(this); } @Override public void onActivityIdSet(int activityId) { // 在这里可以使用 ActivityID Log.d("MainActivity", "Activity ID: " + activityId); } } ``` 3. 在非 `Activity` 类中定义一个成员变量,用于保存 `OnActivityIdSetListener` 对象,并在需要使用 `Activity` 的 ID 时,调用该对象的方法获取 `Activity` 的 ID。例如: ```java public class NonActivityClass { private OnActivityIdSetListener mOnActivityIdSetListener; public void setOnActivityIdSetListener(OnActivityIdSetListener listener) { mOnActivityIdSetListener = listener; // 在这里获取 ActivityID 并传递给 OnActivityIdSetListener 对象 if (mOnActivityIdSetListener != null) { mOnActivityIdSetListener.onActivityIdSet(getActivityId()); } } private int getActivityId() { Activity activity = (Activity) mOnActivityIdSetListener; if (activity != null) { return activity.hashCode(); } return 0; } } ``` 通过上述步骤,即可在非 `Activity` 类中获取 `Activity` 的 ID 并使用。需要注意的是,在使用 `Activity` 的 ID 时需要确保 `Activity` 已经创建完成,否则可能会出现空指针异常等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值