【问题标题】:how to toast inside timerTask run.Exception is uncaught handler如何在 timerTask run.Exception 内敬酒是未捕获的处理程序
【发布时间】:2015-04-23 06:08:26
【问题描述】:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            time=Long.parseLong(intent.getStringExtra("time"));
    Toast.makeText(this, ""+time, Toast.LENGTH_LONG).show();
    timer=new Timer();
    TimerTask timerTask=new TimerTask() {
        @Override
        public void run() {
            Calendar c = Calendar.getInstance();
            int minute = c.get(Calendar.MINUTE);
            if(minute==time) {

                Log.d("AlarmService", "timer");
                Toast.makeText(getApplicationContext(),"ringing",Toast.LENGTH_SHORT);
            }
        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, 1000*2);

    return super.onStartCommand(intent, flags, startId);
}

【问题讨论】:

标签: android


【解决方案1】:

如何在 timerTask run.Exception 中吐司是未捕获的处理程序

因为在非 ui 线程上执行的TimerTask 的 run 方法显示 Toast。

所以要解决问题,请使用HandlerrunOnUiThread 显示来自run 方法的Toast

【讨论】:

    【解决方案2】:

    你没用过

    .show();
    

    在你的吐司里。

    这应该是

    Toast.makeText(getApplicationContext(),"ringing",Toast.LENGTH_SHORT).show();
    

    代替

    Toast.makeText(getApplicationContext(),"ringing",Toast.LENGTH_SHORT);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 2014-09-02
      • 2021-10-06
      相关资源
      最近更新 更多