0

I read that views created from UI thread can't be accessed from another thread directly without using post() method utilizing looper/Handler or RunonUI() method. This is for security reasons. I tried to test this and created a test class as below but I am able to change values of views from a new thread directly. Is there any gap in my understanding or doing something wrong ?

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class RemoteBoundActivity extends AppCompatActivity {

    private static final String TAG = "RemoteBoundActivity";
    TextView numbertTextView = null;
    Button button = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote_bound);
        numbertTextView = (TextView) findViewById(R.id.numbertTextView);
        button = findViewById(R.id.startTask);
    }

    public void startTask(View view) {
        new WorkerThread().start();
    }

    public void stopTask(View view) {
        Log.i(TAG, "stopTask executed");
        Toast.makeText(getBaseContext(), "Task Stopped", Toast.LENGTH_SHORT).show();
    }

    private class WorkerThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                SystemClock.sleep(1000);
                numbertTextView.setText(i+"%");
                button.setText(i+"%");
                Log.i(TAG, "startTask: Thread ID : "+Thread.currentThread().getId()+" value :"+i);
            }
        }
    }


}
  • Hey is the sleep method in the SystemClock class some kind of timer with a one second interval? –  May 08 '21 at 14:03
  • I tried this and it does not work, it says only the thread that created a view can access it –  May 08 '21 at 14:42
  • @CodeTiger yes SystemClock's sleep method is same as Thread.sleep, only difference is that we do not have to explicitly put this in try catch, it internally handles exception. – rajiv kumar May 08 '21 at 15:24
  • @CodeTiger For me this was working fine, not sure if due to SDK different i.e. target API. Which version you are running with ? – rajiv kumar May 08 '21 at 15:25
  • target API is 29 and the version is android 10 –  May 08 '21 at 15:37
  • you defined the worker thread inside the main class of your project, i see the class and other methods are indented equally so they are in the same hierarchy as your class methods? –  May 08 '21 at 15:42
  • I did not see that lemme retry with private class in same level as main class methods and see if any changes will be thrown –  May 08 '21 at 15:44

1 Answers1

0

You can user Handler, AsyncTask and very ease user runOnUiThread() I hope it's userful

LuongXuanNam
  • 67
  • 1
  • 4