正如标题所示,我不确定我创建的是否是一个单独的线程。我使用过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或其他东西,但实际上没有简明的教程解释它们如何工作以及如何创建它们。 任何帮助表示赞赏。
答案 0 :(得分:1)
不,你没有。 Handler附加到当前线程的Looper,在本例中是UI线程。如果要将其分配给其他线程,可以使用以下代码:
HandlerThread thread = new HandlerThread("ThreadName");
thread.start();
hand = new Handler(thread.getLooper());
hand是你的Handler对象
答案 1 :(得分:1)
我的代码与正常运行的线程(我删除了大部分特定于我的应用程序的其他代码)。享受!
Handler hand = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HandlerThread thread = new HandlerThread("ThreadName");
thread.start();
hand = new Handler(thread.getLooper());
hand.postDelayed(run, 1000);
}
Runnable run = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
longSwordPerSecond();
}
};
public void longSwordPerSecond() {
if (goldCount>1)
goldCount += 0.1f;
runOnUiThread(new Runnable() {
@Override
public void run() {
textGoldCount.setText(goldCount + " Gold");
}
});
hand.postDelayed(run, 1000);
}
“runonuithread”用于更新UI。所以数学是在线程中完成的,UI所做的唯一一点是显示结果。这样,UI就不会被进程锁定!