Question

As the title reads, I am unsure if what I have created is a separate thread or not. I have used a Handler, and a Runnable and such, but I don't know what counts as a thread and what doesn't.

My code:

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);


}

I've also been told to use a HandlerThread or something, but there are virtually no concise tutorials around explaining how they work and how to create them. Any help is appreciated.

Était-ce utile?

La solution

No, you have not. Handler is attached to the Looper of the current thread, in this case the UI thread. If you want to assign it to a different thread, you can use the following code:

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

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

Where hand is your Handler object

Autres conseils

My code with functioning thread (I removed most of the other code specific to my app). Enjoy!

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);


}

The "runonuithread" is used to update the UI. So the math is done in the thread, and the only bit the UI does is show the result. That way the UI is not locked up by the process!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top