2011-03-07 44 views
5

我想显示内部计时器吐司消息,我用下面的代码:如何在计时器内显示吐司?

timer.scheduleAtFixedRate(new TimerTask() 
{  
public void run() 
{ 
    try { 
     fun1(); 
     } catch (Exception e) {e.printStackTrace(); }    
    } 
}, 0,60000);  

public void fun1() 
{ 
    //want to display toast 
} 

而我得到以下错误:

WARN/System.err的(593):了java.lang.RuntimeException:不能内螺纹已不叫Looper.prepare创建处理程序()

WARN/System.err的(593):在android.os.Handler(Handler.java:121)

WARN /系统.err(593):在android.widget.Toast。(Toast.java:68)

WARN/System.err的(593):在android.widget.Toast.makeText(Toast.java:231)

感谢。

+0

我把所有这些信息放在一个完整的工作代码,看到这个http://stackoverflow.com/questions/11906361/how-to-display-a-toast-in-a-timer – Sourav301 2012-08-10 17:49:08

回答

5

你不能让UI里面更新单独的线程中,如定时器。你应该使用Handler对象进行UI更新:

timer.scheduleAtFixedRate(new TimerTask() { 
private Handler updateUI = new Handler(){ 
@Override 
public void dispatchMessage(Message msg) { 
    super.dispatchMessage(msg); 
    fun1(); 
} 
}; 
public void run() { 
try { 
updateUI.sendEmptyMessage(0); 
} catch (Exception e) {e.printStackTrace(); } 
} 
}, 0,60000); 
+0

Thanks..it帮助了我.. :) – Monali 2011-03-07 13:05:38

0

在此创建

private Handler handler = new Handler() { 
     public void handleMessage(android.os.Message msg) { 
      // Toast here 
     } 
    }; 
+0

仍然我越来越同样的错误 – Monali 2011-03-07 11:34:30

0

您需要访问应用程序的语境下才能够做这Handler和显示敬酒。尝试创建自己的类这需要上下文作为输入参数:然后在你的计时器

private class MyTimerTask extends TimerTask { 
    private Context context; 
    public MyTimerTask(Context context) { 
     this.context = context; 
    } 

    @Override 
    public void run() { 
     Toast.makeText(context, "Toast text", Toast.LENGTH_SHORT).show(); 
    } 

} 

timer.scheduleAtFixedRate(new MyTimerTask(this), 0,60000); 
+0

这不适合我!它仍然给出了同样的错误: java.lang.RuntimeException:无法在未调用Looper.prepare()的线程中创建处理程序() – robguinness 2012-08-24 10:51:26

0

我想用我自己的观点做我自己的敬酒。

我已经成功地将您的方法结合在一起。以下代码允许我显示吐司并更改/删除视图而不会崩溃,只需将构造函数的参数更改为需要处理的任何内容即可。

public void yourFunction(){ 

    Timer timer = new Timer(); 
    MyTimerTask mtc = new MyTimerTask(this.getContext(), tvNotice); 
    timer.schedule(mtc, 1000); 
} 

private class MyTimerTask extends TimerTask { 

    private TextView tv; 
    private Context context; 
    public MyTimerTask(Context pContext, TextView pTv) { 
     this.tv = pTv; 
     this.context = pContext; 
    } 
    @Override 
    public void run() { 
     updateUI.sendEmptyMessage(0); 
    } 

    private Handler updateUI = new Handler(){ 
     @Override 
     public void dispatchMessage(Message msg) { 
      super.dispatchMessage(msg); 


      tv.setText("TextView Message"); 
      Toast.makeText(context, "Toast Message", 0).show(); 
     } 
    }; 
} 
0

我想做一个简单的项目,可以在计时器中显示吐司。 计时器将使用服务启动。然后,定时器在服务启动时启动,在服务停止时停止。

1类

package com.example.connect; 


import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity { 

Button button1,button2; 
private Handler mHandler = new Handler(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    button1=(Button)findViewById(R.id.button1); 
    button2=(Button)findViewById(R.id.button2); 


} 


public void Start(View v) 
{ 
    startService(new Intent(MainActivity.this , Connect_service.class)); 

} 

public void Stop(View v) 
{ 
    stopService(new Intent(MainActivity.this , Connect_service.class)); 

} 

} 

2类

package com.example.connect; 

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class Connect_service extends Service{ 

Timer timer = new Timer(); 
TimerTask updateProfile = new CustomTimerTask(Connect_service.this); 

public void onCreate() { 

    super.onCreate(); 

    Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show(); 
    timer.scheduleAtFixedRate(updateProfile, 0, 5000); 

} 


@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show(); 
    timer.cancel(); 
} 



@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

} 

3类

package com.example.connect; 

import java.util.TimerTask; 

import android.content.Context; 
import android.os.Handler; 
import android.widget.Toast; 
public class CustomTimerTask extends TimerTask { 


private Context context; 
private Handler mHandler = new Handler(); 

public CustomTimerTask(Context con) { 
    this.context = con; 
} 



@Override 
public void run() { 
    new Thread(new Runnable() { 

     public void run() { 

      mHandler.post(new Runnable() { 
       public void run() { 
        Toast.makeText(context, "In Timer", Toast.LENGTH_SHORT).show(); 

       } 
      }); 
     } 
    }).start(); 

} 

} 
2

最简单的方法(IMO)是:

new Timer().scheduleAtFixedRate(new TimerTask() { 
    @Override 
    public void run() { 
     final String message = "Hi"; 
     MyActivity.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(MyActivity.this, message, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
}); 

MyActivity.this.runOnUiThread(Runnable)的关键。