-1

I have one App that uses Google maps, I am trying to get Duration data by fetching JSON data through AsyncTask...and testing it on my Tab...

The AsyncTask is not working and executing here...

With some search i get to know that AsyncTask do not work proper in 4.0.4 so u better to use runnable instead of AsyncTask

Now here i dont have any idea of runnable or handler and all...

please help me how and where and what i have to code to work my AsyncTask ?

public class Map extends Activity {

    GoogleMap mMap;
    String duration,distance,url,s;
    StringBuilder data = null;
    InputStream is = null;
    JSONObject jObj = null;
    String json = "";
    ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());


            //Toast.makeText(getApplicationContext(), "Cant connect to google play service", 10).show();    
            MapFragment fa = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment1);
            mMap = fa.getMap();

            new dis().execute();
            //Toast.makeText(getApplicationContext(), duration, 10).show();


    }

    public class dis extends AsyncTask<Void, Void, Void>    {

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            try{
                Toast.makeText(getApplicationContext(), "jinal", 10).show();
            }catch(Exception e){}

            return null;
            }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub

            pd.dismiss();
            Toast.makeText(getApplicationContext(), "jinal", 10).show();
                super.onPostExecute(result);


        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pd = new ProgressDialog(Map.this);
            pd.setTitle("Please Wait....");
            pd.setMessage("Wait");
            pd.setCancelable(false);
            pd.show();
        }


    }


}

right now i am just implimenting simple code, but its not giving output

Jinal
  • 1
  • 2
  • I'm not sure where you got the idea or what exactly you mean by "AsyncTask do not work proper in 4.0.4" but if you post the relevant code, we can probably help you – codeMagic Feb 06 '15 at 17:42
  • Post the asynctask code which you have right now – Psypher Feb 06 '15 at 17:42
  • http://stackoverflow.com/questions/12839384/asynctask-doinbackground-not-called-in-android-tablet – Jinal Feb 06 '15 at 17:52
  • U can check this link – Jinal Feb 06 '15 at 17:54
  • 10 as Toast duration ? I never tried that! Is it supposed to work ? http://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context,%20int,%20int) – Gorcyn Feb 06 '15 at 18:01
  • Can't do UI stuff in `doInBackground()`. Toast is shown on the UI – codeMagic Feb 06 '15 at 18:18
  • Also, as @Gorcyn mentioned, you can't use that duration. You need to use one of two Constants of Toast, last I knew – codeMagic Feb 06 '15 at 18:27

2 Answers2

0

You should put a breakpoint in your try/catch statement in doInBackground()method, and see if it actually executes. Then put a e.printStackTrace() inside of your catch block, to see if there is an exception!

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
0

Toast needs the Activity context, not the Application one

You should try with:

Toast.makeText(Map.this, "jinal", Toast.LENGTH_SHORT).show();
Gorcyn
  • 2,807
  • 1
  • 20
  • 22