如何制作新线程Android Studio? [英] How to make a new thread, Android Studio?

查看:109
本文介绍了如何制作新线程Android Studio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下片段类:

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

    }
}

会更新玩家币种的UI显示.但是,由于后台运行着许多不同的进程,因此该线程滞后并出现问题.如何在单独的线程上运行此程序以避免这种情况?

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?

我尝试过这样的事情:

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

但这没有帮助!

谢谢!

推荐答案

我遇到了同样的问题.

我的问题的解决方案是创建3个可运行对象,并在 onCreate()方法中启动它们.

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

看起来像这样:

Thread 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
    }
}; 

如果您想要一些延迟的操作,则可以在可运行的界面中处理延迟的发布(请注意, postDelayed()只会重复整个可运行的内容.您可以通过添加一些条件来避免这种情况)

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

如果要更新GUI,则应在可运行对象内部调用以下命令:

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.如果您有多个线程,并且必须在不同的时间启动它们,则可以从Runnables启动它们.

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

一些有用的页面:

新的Runnable()但没有新的线程吗?

Thread start()和Runnable run有什么区别()

如何在Android是否有规定的时间间隔?

何时使用handler.post()&何时使用新的Thread()

这篇关于如何制作新线程Android Studio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆