toast消息在服务意图中不起作用

时间:2014-01-15 09:57:32

标签: android toast

有没有人知道为什么下面的吐司没有出现? 打印日志,同一功能中的另一个吐司也正常工作

    if (searchType.equals("ByName")){
        // get the extra from the intent:
        String query = intent.getStringExtra("query");      
            if (!searchByName(query)){
                Toast.makeText(getApplicationContext(), "No results for this search",
                        Toast.LENGTH_LONG).show();  
                Log.d(TAG, "return status true/false -->" + searchByName(query));
            }           
    }

任何帮助将不胜感激

我正在附加日志,我不得不删除一些行,因为它太长了... 这是日志:

01-15 10:22:39.267: D/FragmentList(2035): doSearchByName
01-15 10:22:39.267: D/FragmentList(2035): query -> asdgasdgsdagas
01-15 10:22:39.271: D/FragmentList(2035): ByName
01-15 10:22:39.283: D/FragmentList(2035): currentLocation
01-15 10:22:39.303: D/SearchPlacesSevice(2035): onHandleIntentstart
01-15 10:22:39.303: D/SearchPlacesSevice(2035): ByName
01-15 10:22:39.303: D/SearchPlacesSevice(2035): searchByName
01-15 10:22:39.303: D/GooglePlaces(2035): query -> asdgasdgsdagas
01-15 10:22:39.307: D/GooglePlaces(2035): https://maps.googleapis.com/maps/api/place/textsearch/json?query=asdgasdgsdagas&sensor=false&key=AIzaSyBDi3A3ZLFQPm1I9sUsYasxtwndQjn7mfQ
01-15 10:22:39.879: D/GooglePlaces(2035): {
01-15 10:22:39.879: D/GooglePlaces(2035):    "debug_info" : [],
01-15 10:22:39.879: D/GooglePlaces(2035):    "html_attributions" : [],
01-15 10:22:39.879: D/GooglePlaces(2035):    "results" : [],
01-15 10:22:39.879: D/GooglePlaces(2035):    "status" : "ZERO_RESULTS"
01-15 10:22:39.879: D/GooglePlaces(2035): }
01-15 10:22:39.879: D/SearchPlacesSevice(2035): {
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "debug_info" : [],
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "html_attributions" : [],
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "results" : [],
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "status" : "ZERO_RESULTS"
01-15 10:22:39.879: D/SearchPlacesSevice(2035): }
01-15 10:22:39.879: D/SearchPlacesSevice(2035): ZERO_RESULTS
01-15 10:22:39.883: D/SearchPlacesSevice(2035): Status -->ZERO_RESULTS
01-15 10:22:39.887: D/SearchPlacesSevice(2035): searchByName
01-15 10:22:39.887: D/GooglePlaces(2035): query -> asdgasdgsdagas
01-15 10:22:39.887: D/GooglePlaces(2035): https://maps.googleapis.com/maps/api/place/textsearch/json?query=asdgasdgsdagas&sensor=false&key=AIzaSyBDi3A3ZLFQPm1I9sUsYasxtwndQjn7mfQ
01-15 10:22:40.083: D/SearchPlacesSevice(2035): {
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "debug_info" : [],
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "html_attributions" : [],
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "results" : [],
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "status" : "ZERO_RESULTS"
01-15 10:22:40.083: D/SearchPlacesSevice(2035): }
01-15 10:22:40.087: D/SearchPlacesSevice(2035): ZERO_RESULTS
01-15 10:22:40.087: D/SearchPlacesSevice(2035): Status -->ZERO_RESULTS
01-15 10:22:40.091: D/SearchPlacesSevice(2035): return status true/false -->false
01-15 10:22:43.387: W/MessageQueue(2035): Handler (android.os.Handler) {535afd88} sending message to a Handler on a dead thread
01-15 10:22:43.387: W/MessageQueue(2035): java.lang.RuntimeException: Handler (android.os.Handler) {535afd88} sending message to a Handler on a dead thread
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Handler.post(Handler.java:263)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.widget.Toast$TN.hide(Toast.java:363)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:55)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Binder.execTransact(Binder.java:367)
01-15 10:22:43.387: W/MessageQueue(2035):   at dalvik.system.NativeStart.run(Native Method)
01-15 10:23:42.003: D/dalvikvm(2035): GC_CONCURRENT freed 434K, 6% free 8502K/9031K, paused 1ms+1ms, total 8ms

1 个答案:

答案 0 :(得分:0)

我认为它是Android Framework中的一个错误。每当您尝试从应用程序主线程中制作或显示Toast时。最常使用IntentService。始终确保您的Toast进入应用程序主线程。

请查看以下解决方法。所有代码都在您的服务中。

处理程序声明。

Handler handler = null;

并在您的服务的构造函数中

handler  = new Handler();

调用显示Toast的方法。

public void showToast(String message, Context context){
        handler.post(new CustomToast(message, context));
    }

CostomToast

private class CustomToast implements Runnable{
          String mText;
          Context mContext;

  public CustomToast(String text, Context context){
            mText = text;
            mContext = context;
  }

   public void run(){
            Toast.makeText(mContext, mText, Toast.LENGTH_LONG).show();
  }