gpt4 book ai didi

java - 我创建了一个线程吗?

转载 作者:行者123 更新时间:2023-12-02 05:51:42 26 4
gpt4 key购买 nike

正如标题所示,我不确定我创建的是否是一个单独的线程。我使用过 Handler 和 Runnable 等,但我不知道什么算作线程,什么不算。

我的代码:

Handler hand = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set full-screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.mainlayout);

// Initialize variables from popup window
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.storemenu,
null, false));
storeDismiss = (Button) pw.getContentView().findViewById(
R.id.menudismissid);

prefs = getSharedPreferences("LeagueClicker", Context.MODE_PRIVATE);

goldCount = (long) prefs.getFloat("goldCount", 0.0f);

textGoldCount = (TextView) findViewById(R.id.textviewtop);
textGoldCount.setText(goldCount + " Gold");
textGoldCount.setGravity(Gravity.CENTER);
Typeface tf = Typeface.createFromAsset(getAssets(), "mechanical.ttf");
textGoldCount.setTypeface(tf);
textGoldCount.setTextSize(35);

// Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
storeClick = (Button) findViewById(R.id.storeimageid);

spinningBars = (ImageView) findViewById(R.id.spinningbarsid);

// Setting onClickListeners
minionClick.setOnClickListener(this);
storeClick.setOnClickListener(this);
storeDismiss.setOnClickListener(this);

// Begin animation of bars
Animation rotation = AnimationUtils.loadAnimation(this,
R.anim.spinningbarsanimation);
rotation.setRepeatCount(Animation.INFINITE);
spinningBars.startAnimation(rotation);

hand.postDelayed(run, 1000);

/*goldPerSecondMethod();*/


}

Runnable run = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
longSwordPerSecond();
}
};

public void longSwordPerSecond() {
if (goldCount>1)
goldCount += 0.1f;
textGoldCount.setText(goldCount + " Gold");
textGoldCount.setGravity(Gravity.CENTER);
hand.postDelayed(run, 1000);


}

我还被告知要使用 HandlerThread 或其他东西,但实际上没有简明的教程来解释它们如何工作以及如何创建它们。如有任何帮助,我们将不胜感激。

最佳答案

不,你没有。 Handler 附加到当前线程(在本例中为 UI 线程)的 Looper。如果想将其分配给不同的线程,可以使用以下代码:

    HandlerThread thread = new HandlerThread("ThreadName");
thread.start();

hand = new Handler(thread.getLooper());

你的 Handler 对象在哪里

关于java - 我创建了一个线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23479848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com