标签: android-threading

AsyncTask和Thread/Runnable之间的区别

我有疑问让我感到困惑.

想象一下,我想在另一个线程中做一些事情,比如获取GPS /位置内容,这些内容按照SDK文档中的建议,必须使用后台线程.

所以这里有一个问题:两者之间有什么区别

  1. Thread通过AND在后台创建AsyncTask

  2. 创建Thread thread1 = new Thread(new Runnable()......并实施run()

multithreading android runnable android-asynctask android-threading

57
推荐指数
1
解决办法
3万
查看次数

Android CoroutineScope 完成后自动取消

我想知道coroutineScope工作完成后是否会自动取消。coroutineScope假设我在自定义类中创建一个而不是 ViewModel类或Fragment / Activity类:

class MyClass {
    private val backgroundScope = CoroutineScope(Dispatchers.Default)

    fun doSomething() = backgroundScope.launch {
        //do background work
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,后台工作完成后,会backgroundScope自动取消吗?

android kotlin kotlin-coroutines android-threading coroutinescope

6
推荐指数
2
解决办法
3591
查看次数

如何创建一个新线程,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 …
Run Code Online (Sandbox Code Playgroud)

java android android-fragments android-studio android-threading

2
推荐指数
1
解决办法
9607
查看次数