How to make a new thread, Android Studio?

余生长醉 提交于 2019-12-11 07:48:19

问题


I have the following fragment class:

public class fragment1 extends Fragment {
    private TextView bunz_count;
    private TextView money_count;
    private Bunz bunz;
    private Handler handler;
    int delay = 1000;
    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        bunz = Bunz.getInstance();
        handler = new Handler();


        view = inflater.inflate(R.layout.fragment1, container, false);


        handler.postDelayed(new Runnable(){
            public void run(){
                update(view);
                handler.postDelayed(this, delay);
            }
        }, delay);







        return view;
    }


    public void update(View view){
        bunz_count = (TextView) view.findViewById(R.id.final_bunz_count);
        money_count = (TextView) view.findViewById(R.id.final_money_count);
        //System.out.println(bunz.getBaker1());


        BigDecimal number = ((BigDecimal.valueOf
                (bunz.getBaker1()).multiply(BigDecimal.valueOf(.1))));
//        ).add((BigDecimal.valueOf(bunz.getBaker2()).multiply(BigDecimal.valueOf(.2)))).
//                add((BigDecimal.valueOf
//                        (bunz.getBaker3()).multiply(BigDecimal.valueOf(.4)))).
//                add((BigDecimal.valueOf
//                        (bunz.getBaker4()).multiply(BigDecimal.valueOf(.8)))).
//                add((BigDecimal.valueOf(bunz.getBaker5()).multiply(BigDecimal.valueOf(1)))).
//                add((BigDecimal.valueOf(bunz.getBaker6()).multiply(BigDecimal.valueOf(2)))).
//                add((BigDecimal.valueOf(bunz.getBaker7()).multiply(BigDecimal.valueOf(4)))).
//                add((BigDecimal.valueOf(bunz.getBaker8()).multiply(BigDecimal.valueOf(5)))).
                //add((BigDecimal.valueOf(bunz.getBaker9()).multiply(BigDecimal.valueOf(10))));
        //System.out.println(number);
        bunz.setBunz(bunz.getBunz().add((number)));
        bunz_count.setText("Bunz: " + bunz.getBunz());
        money_count.setText("Money: " + bunz.getMoney());
        System.out.println("bunz" + bunz.getBunz());

    }
}

which updates the UI display of a players currency. However, since there are lots of different processes running in the background, this thread lags and has problems. How can I run this on a separate thread to avoid this?

Edit: I tried something like this:

        mHandlerThread = new HandlerThread("yeye");
        mHandlerThread.start();
        handler = new Handler(mHandlerThread.getLooper());
        handler.postDelayed(new Runnable(){
            public void run(){
                update(view);
                handler.postDelayed(this, delay);
            }
        }, delay);

but it didn't help!

Thanks!


回答1:


I have encountered the same problem.

The solution for my issue was to create 3 runnables and start them in onCreate() Method.

It looks like that:

Thrad thread = new Thread(runnable);
thread.start();

In order to create runnable "object" just do the following:

Runnable runnable = new Runnable(){
    public void run() {   
       //some code here
    }
}; 

If you want some delayed action, you can work with post delayed in runnable interface (beware, postDelayed() would just repeat the whole runnable. You can avoid that, by adding some conditions)

Runnable runnable = new Runnable(){
    public void run() {   
       //some code here
       handler.postDelayed(this, 1000);
    }
}; 

If you want to update GUI, you should invoke following command inside your runnable:

handler.post(new Runnable() {
     @Override
     public void run () {
     // upate textFields, images etc...

     }
});

P.S. If you have multiple threads and they must be started at different time you can start them from Runnables.

Some pages that can be helpful:

new Runnable() but no new thread? and What's the difference between Thread start() and Runnable run() and How to run a Runnable thread in Android at defined intervals? and When to use handler.post() & when to new Thread()



来源:https://stackoverflow.com/questions/57697796/how-to-make-a-new-thread-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!