从Android中Service类的线程访问Activity上的UI组件

问题描述

|| 我有一个由MyService命名的类,该类在下面扩展了Service。一切都会跑到 我在Thread的run方法中删除了“ 0”行。 为什么?以及如何从Thread类的run方法访问Activity组件?
public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) { return null; }

@Override
public void onCreate() {
    Toast.makeText(this,\"This msg will be shown\",Toast.LENGTH_LONG).show();
    Log.d(\"Bilgi\",\"This msg will be shown.\");
    super.onCreate();
}

@Override
public void onStart(Intent intent,int startId) {
    Toast.makeText(this,Toast.LENGTH_LONG).show();
    super.onStart(intent,startId);

    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            try {
                Log.d(\"This msg will \",\"be shown\"); //if I remove next line 
                Toast.makeText(this,\"This msg will NOT be shown\",Toast.LENGTH_LONG).show(); 

                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    },5000,8000);
}
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)