2014-06-21 57 views
0

我有類GPSTracker擴展Service實現LocationListener。 Insinde onLocationChanged(),我有一個線程,我想顯示吐司「ABC」,我該怎麼做? Plz幫助我?如何在服務實現LocatonListener的線程中顯示Toast?

public void onLocationChanged(Location location) { 
     //Log.d(TAG, "Your Location is - \nLat: " + latitude + "\nLong: " + longitude); 
     // update location here 
     String regChildID = SharePreferenceData.getCheckedRegister(mContext); 

      if (regChildID.split(",")[0].equalsIgnoreCase("1")) { 

       try { 
        String locationData; 
        String regID = regChildID.split(",")[2]; 
        locationData = URLEncoder.encode("reg_child_id", "UTF-8") + "=" + URLEncoder.encode(regID.substring(0, regID.length()-1), "UTF-8"); 
        locationData += "&" + URLEncoder.encode("lat", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(latitude), "UTF-8"); 
        locationData += "&" + URLEncoder.encode("long", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(longitude), "UTF-8"); 
        numberRequest = numberRequest +1; 
        final String data = locationData; 
        new Thread(new Runnable() { 

         @Override 
         public void run() { 
          String response = HttpRequest.sendData(Def.HTTP_METHOD_POST, Def.LOCATION_API, data); 
          if (response != null) { 
           //Log.d(TAG, "Response from server: " + data); 
           Log.d(TAG, "Response from server: " + response); 
           Log.d(TAG, "Number request update: " + numberRequest); 
           new Handler(Looper.getMainLooper()).post(new Runnable() { 

            @Override 
            public void run() { 
             Toast.makeText(getApplicationContext(), "asd", Toast.LENGTH_SHORT).show(); 
            } 
           }); 
          }else { 
           Log.d(TAG, "Number request update: " + "No response from server"); 
          } 
         } 
        }).start(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

     } 

==================================>更新我的解決方案

謝謝大家,我剛剛做到了。我把,我在構造器類擴展服務調用上下文,內螺紋我做這樣的代碼,謝謝@busylee

new Thread(new Runnable() { 

         @Override 
         public void run() { 
          ... 
        new Handler(Looper.getMainLooper()).post(new Runnable() { 

            @Override 
            public void run() { 
             Toast.makeText(mContext, "abc", Toast.LENGTH_SHORT).show(); 
            } 
           }); 
          ... 
        }).start(); 
+0

不,我想是不是酷的傢伙什麼的,但你確定要從服務中顯示吐司? –

回答

2

所有你需要定義Handler內螺紋你服務正在運行,並張貼Runnable它。此代碼可以幫助你:

Handler handler = new Handler(); 

new Thread(new Runnable() { 

         @Override 
         public void run() { 
          ... 
          handler.post(new Runnable() { 

            @Override 
            public void run() { 
             Toast.makeText(getApplicationContext(), "asd", Toast.LENGTH_SHORT).show(); 
            } 
          ... 
        }).start(); 

希望它可以幫助

+0

謝謝,但getApplicationContext時我仍然收到null! –

+0

謝謝你,而不是得到ApplicationContext(),我把一個上下文稱爲構造函數。它工作正常! –

0

可以顯示敬酒消息,就像那個..

new Thread(new Runnable() { 

     @Override 
     public void run() { 


      //your code is here 


      MainActivity.this.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 


         Toast.makeText(context, "Your messsage", Toast.LENGTH_LONG).show();     



       } 
      }); 
     } 
    }).start(); 
+0

上面的代碼在Service中運行。你在哪裏採取MainActivity.this? – busylee

+0

我知道,但實際上我們不能在服務中運行OnUI! –