I decided to deal with such technology as AndroidAnnotations .
In the declared functional there are two such annotations :
@Background - starts an external stream, this thing worked correctly.
@UIThread - starts a thread in the UI, this thing refuses to work.
I tried using the standard solution via runOnUiThread () instead of @UIThred, it worked perfectly.

Error when using @UIThread:

E/AndroidRuntime: FATAL EXCEPTION: pool-3-thread-1 Process: evilroach.com.test, PID: 27529 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

Classes in which I try to use annotations:

 package evilroach.com.test.presenter; import android.annotation.TargetApi; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.UiThread; import android.support.v4.app.NotificationCompat; import android.widget.Toast; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.EBean; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Random; import evilroach.com.test.R; import evilroach.com.test.activity.HomePage; import evilroach.com.test.activity.NotificationView; import evilroach.com.test.model.Person; import io.realm.Realm; import io.realm.RealmConfiguration; import io.realm.RealmResults; @EBean public class Connection { private HomePage homePage; public Connection(Context homePage) { this.homePage = (HomePage) homePage; } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public void checkConnection() { ConnectivityManager connectivityManager = (ConnectivityManager) homePage.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo info; if (networks.length == 0) { makeToast("No Networks"); } else { for (Network network : networks) { info = connectivityManager.getNetworkInfo(network); if (info.getState().equals(NetworkInfo.State.CONNECTED)) { makeToast("Connected."); checkSiteAvailable("http://www.android.com/"); } else { makeToast("Not connected."); } } } } public void notify(String title, String text) { int notificationId = 1; NotificationManager notificationManager = (NotificationManager) homePage.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(homePage.getApplicationContext()). setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setContentText(text).setAutoCancel(true); Intent notificationIntent = new Intent(homePage.getApplicationContext(), NotificationView.class); PendingIntent pendingNotificationIntent = PendingIntent.getActivity(homePage.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.setContentIntent(pendingNotificationIntent); notificationManager.notify(notificationId, notificationBuilder.build()); } public void makeToast(String text) { Toast.makeText(homePage.getApplicationContext(), text, Toast.LENGTH_SHORT).show(); } public void createPreferences() { SharedPreferences preferences = homePage.getPreferences(Context.MODE_APPEND); SharedPreferences.Editor editor = preferences.edit(); editor.putString("name", "Cat" + new Random().nextInt(100)); editor.apply(); } public void readPreferences() { SharedPreferences preferences = homePage.getPreferences(Context.MODE_PRIVATE); String name = preferences.getString("name", "No name"); homePage.writePreferences(name); } @Background public void checkSiteAvailable(String link) { final int TIMEOUT = 1000; String result = null; URL url; HttpURLConnection connection = null; try { url = new URL(link); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(TIMEOUT); connection.connect(); result = "Connected to: " + link; } catch (IOException e) { result = "Connecting failed."; } finally { if (connection != null) { connection.disconnect(); } } displayResultsOnUI(result); } /*public void displayResultsOnUI(final String results) { homePage.runOnUiThread(new Runnable() { @Override public void run() { homePage.showInfo(results); } }); }*/ //Tried to use @UIThread public void displayResultsOnUI(String results) { homePage.showInfo(results); } public int countRealmObjects(){ RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(homePage).build(); Realm realm = Realm.getInstance(realmConfiguration); Person personMax = new Person("Max", "Sych", 30); Person personSlava = new Person("Slava", "Martynenko", 20); Person personLeha = new Person("Leha", "Samoilov", 40); realm.beginTransaction(); realm.copyToRealm(personMax); realm.copyToRealm(personSlava); realm.copyToRealm(personLeha); realm.commitTransaction(); RealmResults<Person> persons = realm.where(Person.class).lessThan("age", 35).findAll(); return persons.size(); } } 

and,

 package evilroach.com.test.activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore; import android.support.annotation.UiThread; import android.support.v7.app.AppCompatActivity; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.squareup.otto.Subscribe; import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EActivity; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; import evilroach.com.test.R; import evilroach.com.test.model.Person; import evilroach.com.test.presenter.Connection; import evilroach.com.test.service.NewsMessage; import evilroach.com.test.util.otto.BusProvider; import evilroach.com.test.util.otto.ButtonEvent; import io.realm.Realm; import io.realm.RealmConfiguration; import io.realm.RealmResults; @EActivity public class HomePage extends AppCompatActivity { @Bean Connection connection; @Bind(R.id.news_block) TextView newsBlock; @Bind(R.id.buttonMessage) Button messageAlert; @Bind(R.id.buttonInternet) Button internetConnection; @Bind(R.id.buttonImage) Button animation; @Bind(R.id.buttonCamera) Button camera; @Bind(R.id.imageView) ImageView image; @OnClick(R.id.buttonMessage) public void showMaps() { newsBlock.append("\nClick."); connection.readPreferences(); getGoogleMap(); } @OnClick(R.id.buttonInternet) public void checkInternetConnection() { connection.checkConnection(); connection.createPreferences(); getDaggerActivity(); } @OnClick(R.id.buttonImage) public void AnimateImage() { Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade); image.startAnimation(animation); BusProvider.getInstance().post(new ButtonEvent("Otto!")); writeRealm(); } @OnClick(R.id.buttonCamera) public void takePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); } @Subscribe public void anotherButtonPress(ButtonEvent event) { Toast.makeText(this, event.getText(), Toast.LENGTH_SHORT).show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_page); ButterKnife.bind(this); BusProvider.getInstance().register(this); startService(new Intent(getBaseContext(), NewsMessage.class)); } @Override protected void onStart() { super.onStart(); newsBlock.append("\nShow."); } @Override protected void onStop() { super.onStop(); newsBlock.append("\nHide."); } @Override protected void onDestroy() { super.onDestroy(); stopService(new Intent(getBaseContext(), NewsMessage.class)); connection.notify("By!", "Don`t forget about us."); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Bitmap photo = (Bitmap) data.getExtras().get("data"); image.setImageBitmap(photo); } // Tried to use @UiThread public void showInfo(String info) { newsBlock.append("\n" + info); } public void writePreferences(String pref) { newsBlock.append("\n" + pref); } public void getGoogleMap() { Intent intent = new Intent(this, MapsActivity.class); startActivity(intent); } public void getDaggerActivity() { Intent intent = new Intent(this, DaggerOutputActivity.class); startActivity(intent); } public void writeRealm(){ int count = connection.countRealmObjects(); newsBlock.append("\nRealm objects: " + String.valueOf(count)); } } 

Entire project: gitHub .

    1 answer 1

    The problem was that there were several import options that I did not notice.

     import android.support.annotation.UiThread; 

    need to be replaced by:

     import org.androidannotations.annotations.UiThread;