1/13

Android Development

Android Framework Basics

2/13

Agenda

Komplett überarbeiten: https://developer.android.com/topic/libraries/architecture/workmanager

3/13

UI-Thread

4/13

UI-Thread

5/13

UI-Thread

6/13

UI-Thread

7/13

UI-Thread

8/13

UI-Thread

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
                }
            });
        }
    }).start();
}
9/13

UI-Thread

10/13

Multithreading using AsyncTaskLoader

androidx import

implementation "androidx.loader:loader:1.1.0"
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<String> {

	private static final int LOADER_ID = 42;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Loader<String> loader = getSupportLoaderManager().initLoader(LOADER_ID, null, this);
		if (loader != null) {
			loader.forceLoad();
		}

	}

	@NonNull
	@Override
	public Loader<String> onCreateLoader(int id, @Nullable Bundle args) {
		return new MyDataLoader(MainActivity.this);
	}

	@Override
	public void onLoadFinished(@NonNull Loader<String> loader, String data) {
		TextView textView = findViewById(R.id.myTextView);
		textView.setText(data);
	}

	@Override
	public void onLoaderReset(@NonNull Loader<String> loader) {
		TextView textView = findViewById(R.id.myTextView);
		textView.setText("");
	}
}
11/13

Multithreading using AsyncTaskLoader

import android.content.Context;
import android.os.SystemClock;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.loader.content.AsyncTaskLoader;

class MyDataLoader extends AsyncTaskLoader<String> {

	public MyDataLoader(@NonNull Context context) {
		super(context);
	}

	@Nullable
	@Override
	public String loadInBackground() {
		//do your long running task here

		SystemClock.sleep(2000);

		return "My loaded data";
	}
}
12/13

Summary

13/13

Recab Questions