JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 請教一個Toast的問題
wade_chen





發文: 15
積分: 0
於 2011-03-11 09:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不知到各位有沒遇過這樣的情況,
就是我在TimerTask裡呼叫Toast,
目的是提醒user,我的service已啟動
,但是不知道為什麼我的Toast都不會顯現出來,
我確定run()有跑到因為我有印Log...
所以想請教各位前輩是否有類似的經驗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void schedule(long delay){
        if(timer != null){
            timer.cancel();
        }
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(KitchenTimerservice.this,"running....",Toast.LENGTH_LONG).show();
                //Log.i("Test","***********************");
                sendBroadcast(new Intent(ACTION));
            }
        };
        timer.schedule(timerTask, delay);
    }


reply to postreply to post
作者 Re:請教一個Toast的問題 [Re:wade_chen]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2011-03-11 10:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
應該要在ui thread才可以

全文檢索或查一下官方文件的ui thread
或試試看runOnUiThread

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:請教一個Toast的問題 [Re:koji]
wade_chen





發文: 15
積分: 0
於 2011-03-11 11:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
THX...
我研究一下


reply to postreply to post
作者 Re:請教一個Toast的問題 [Re:wade_chen]
wade_chen





發文: 15
積分: 0
於 2011-03-11 15:28 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
多謝 我解決了,的確是跟UI thread有關

只是我後來是用handler處理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public MyService extends Service{
......
    private Handler mHandler = new Handler(){
        public void handleMessage(Message m) {
            if (m.what == 100)
                Toast.makeText(KitchenTimerservice.this,"running....",Toast.LENGTH_LONG).show();
        }
    };
   ......
    public void schedule(long delay) {
        if (timer != null) {
            timer.cancel();
        }
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
 
            @Override
            public void run() {
                mHandler.sendEmptyMessage(100);
                sendBroadcast(new Intent(ACTION));
            }
        };
        timer.schedule(timerTask, delay);
    }
 
}
 
}

http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html


reply to postreply to post
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8