从服务的Andr​​oid错误调用敬酒 [英] Error calling toast from Service Android

查看:141
本文介绍了从服务的Andr​​oid错误调用敬酒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/4025082/android-toast-started-from-service-only-displays-once">Android吐司开始从服务只显示一次

我使用的服务机器人在android.app.Service定义。

我从活动中调用这个服务(为myService)。

为MyService是:

 公共类为myService延伸服务{

 公众的IBinder onBind(意向意图){
    返回null;
}

公共无效的onCreate(){
    super.onCreate();
    TimerTask的任务=新的TimerTask(){
        公共无效的run(){
            Log.i(测试,服务运行);
            了checkdate();
        }
    };
    定时器=新的Timer();
    timer.schedule(任务,0,20000);
}

 公共无效了checkdate(){
    吐司面包= Toast.makeText(此,Toast.LENGTH_LONG简单的信息!);
    toast.show();
}

}
 

方法了checkdate()所在的班级为myService。

产生的错误是:

  09-19 15:41:35.267:E / AndroidRuntime(2026):致命异常:定时器0
 09-19 15:41:35.267:E / AndroidRuntime(2026):java.lang.RuntimeException的:内螺纹已经不叫尺蠖prepare无法创建处理器()
 09-19 15:41:35.267:E / AndroidRuntime(2026):在android.os.Handler&LT; INIT&GT;(Handler.java:121)
 09-19 15:41:35.267:E / AndroidRuntime(2026):在android.widget.Toast $ TN&LT; INIT&GT;(Toast.java:310)
 09-19 15:41:35.267:E / AndroidRuntime(2026):在android.widget.Toast&LT; INIT&GT;(Toast.java:84)
 09-19 15:41:35.267:E / AndroidRuntime(2026):在android.widget.Toast.makeText(Toast.java:226)
 

解决方案

的TimerTask 运行在一个单独的线程。 Toast.makeText()必须从已经建立了一个处理器/尺蠖一个线程中执行。基本上,这意味着你需要做的吐司上的线程具有标准的Andr​​oid消息/事件调度程序中运行它。

<打击>最容易做的,这将是你的了checkdate()方法方式:

  runOnUiThread(新的Runnable(){
    公共无效的run(){
        吐司面包= Toast.makeText(此,Toast.LENGTH_LONG简单的信息!);
        toast.show();
    }
 });
 

编辑:我是个白痴,这是不对的。不能从服务环境调用runOnUiThread()

您需要使用一个处理程序这一点。在您的服务:

 专用处理器处理器;
 

的onCreate()为您服务的:

 处理程序=新的处理程序();
 

了checkdate()方法:

  handler.post(新的Runnable(){
    公共无效的run(){
        吐司面包= Toast.makeText(myService.this,简单的信息!,Toast.LENGTH_LONG);
        toast.show();
    }
 });
 

Possible Duplicate:
Android Toast started from Service only displays once

I'm using Service Android defined in android.app.Service.

I call this Service (myService) from an Activity.

MyService is:

public class myService extends Service{

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

public void onCreate(){
    super.onCreate();
    TimerTask task = new TimerTask(){
        public void run(){
            Log.i("test","service running");
            checkDate();            
        }           
    };
    timer = new Timer();
    timer.schedule(task, 0, 20000);
}

 public void checkDate(){
    Toast toast = Toast.makeText(this, "SIMPLE MESSAGE!", Toast.LENGTH_LONG);
    toast.show();
}

}

The method checkDate() resides in the class myService.

The error produced is:

 09-19 15:41:35.267: E/AndroidRuntime(2026): FATAL EXCEPTION: Timer-0
 09-19 15:41:35.267: E/AndroidRuntime(2026): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
 09-19 15:41:35.267: E/AndroidRuntime(2026):    at android.os.Handler.<init>(Handler.java:121)
 09-19 15:41:35.267: E/AndroidRuntime(2026):    at android.widget.Toast$TN.<init>(Toast.java:310)
 09-19 15:41:35.267: E/AndroidRuntime(2026):    at android.widget.Toast.<init>(Toast.java:84)
 09-19 15:41:35.267: E/AndroidRuntime(2026):    at android.widget.Toast.makeText(Toast.java:226)

解决方案

TimerTask runs in a separate thread. Toast.makeText() must be executed from a thread that has established a Handler/Looper. Basically this means you need to make the toast on a thread that has the standard Android message/event dispatcher running in it.

Easiest way to do this would be in your checkDate() method:

runOnUiThread(new Runnable() {
    public void run() {
        Toast toast = Toast.makeText(this, "SIMPLE MESSAGE!", Toast.LENGTH_LONG);
        toast.show();
    }
 });

EDIT: I'm an idiot, that's not right. You can't call runOnUiThread() from a Service context

You need to use a Handler for this. In your service:

private Handler handler;

in onCreate() of your service:

handler = new Handler();

in checkDate() method:

handler.post(new Runnable() {
    public void run() {
        Toast toast = Toast.makeText(myService.this, "SIMPLE MESSAGE!", Toast.LENGTH_LONG);
        toast.show();
    }
 });

这篇关于从服务的Andr​​oid错误调用敬酒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆