相关疑难解决方法(0)

Android"只有创建视图层次结构的原始线程才能触及其视图."

我在Android中构建了一个简单的音乐播放器.每首歌曲的视图都包含一个SeekBar,实现方式如下:

public class Song extends Activity implements OnClickListener,Runnable {
    private SeekBar progress;
    private MediaPlayer mp;

    // ...

    private ServiceConnection onService = new ServiceConnection() {
          public void onServiceConnected(ComponentName className,
            IBinder rawBinder) {
              appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer
              progress.setVisibility(SeekBar.VISIBLE);
              progress.setProgress(0);
              mp = appService.getMP();
              appService.playSong(title);
              progress.setMax(mp.getDuration());
              new Thread(Song.this).start();
          }
          public void onServiceDisconnected(ComponentName classname) {
              appService = null;
          }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song);

        // ...

        progress = (SeekBar) findViewById(R.id.progress);

        // ...
    }

    public void run() { …
Run Code Online (Sandbox Code Playgroud)

multithreading android

879
推荐指数
18
解决办法
53万
查看次数

什么是Android UiThread(UI线程)

有人可以向我解释一下UI线程到底是什么吗?在developer.android.com上,它说的是runOnUiThread函数

public final void runOnUiThread(Runnable action)

从以下版本开始:API Level 1在UI线程上运行指定的操作.如果当前线程是UI线程,则立即执行该操作.如果当前线程不是UI线程,则将操作发布到UI线程的事件队列.

UI线程是否意味着每次通过某些ui活动(如来电或屏幕调暗等)将活动推送到后台时都会运行此线程?如果没有,UI线程到底包含什么?

谢谢

android ui-thread

78
推荐指数
3
解决办法
7万
查看次数

标签 统计

android ×2

multithreading ×1

ui-thread ×1