如何在服务器LocatonListener的Thread中显示Toast?

时间:2014-06-21 13:30:27

标签: android

我有类GPSTracker extends Service实现LocationListener。 insinde onLocationChanged(),我有一个帖子,我想要显示Toast“abc”,我该怎么做? Plz帮助我?

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();
                }

        }

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

感谢大家,我刚刚完成了。我把上下文放在我在contructor类中调用的扩展Service,在Thread里面我喜欢这段代码,谢谢@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();

2 个答案:

答案 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();

希望有所帮助

答案 1 :(得分: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();