Skip to content Skip to sidebar Skip to footer

How To Run This Code In A Thread Or Async Task?

When I tried running this code I get an error, about the strictMode Thread policy, so I added this line. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().perm

Solution 1:

You can try something like below.

button1.setOnClickListener(newOnClickListener() {
            @OverridepublicvoidonClick(View arg0) {
                newasyn().execute();
            }
        });

then create your asynctask like this.

classasynextendsAsyncTask<String, String, String> {

        @OverrideprotectedvoidonPreExecute() {
            super.onPreExecute();
            pDialog = newProgressDialog(yourActivityname.this);
            pDialog.setMessage("Loading... Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
        if (InternetStatus.getInstance(this).isOnline(this)) {

XMLParserparser=newXMLParser();
Stringxml= parser.getXmlFromUrl(URL); // getting XML from URLDocumentdoc= parser.getDomElement(xml); // getting DOM element// NodeList nl = doc.getElementsByTagName();NodeListstudentList= doc.getElementsByTagName("streamingurl");

// Print total student elements in document// System.out.println("Total students: " + studentList.getLength());
Toast.makeText(getBaseContext(), "Please wait while stream loads",
        Toast.LENGTH_SHORT).show();

if (studentList != null && studentList.getLength() > 0) {
    for (inti=0; i < studentList.getLength(); i++) {

        Nodenode= studentList.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {

            Elemente= (Element) node;
NodeListnodeList= e.getElementsByTagName("andhigh_value");
    theAndroid_HighValue = nodeList.item(0).getChildNodes()
                    .item(0).getNodeValue();

        }

        vid = (VideoView) findViewById(R.id.videoview);
        vid.setVideoPath(theAndroid_HighValue);
    // static final String KEY_IPADHIGH = "ipadhigh_value";MediaControllermediaController=newMediaController(this);
        mediaController.setAnchorView(vid);
        // vid.setMediaController(mediaController);
        vid.requestFocus();
        vid.start();

    videoBuffering = newProgressDialog(LiveStreaming.this);
        videoBuffering.setMessage("Loading...Please wait");
        // videoBuffering.setIcon(R.drawable.ic_launcher);// videoBuffering.setTitle(R.string.app_name);
    videoBuffering.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        videoBuffering.show();

vid.setOnErrorListener(newOnErrorListener() {
@OverridepublicbooleanonError(MediaPlayer mp, int what,
        int extra) {
        // Log.e(TAG, "Error playing video");// Toast.makeText(getBaseContext(),"No Stream Found",Toast.LENGTH_SHORT).show();AlertDialogNetAlert=newAlertDialog.Builder(LiveStreaming.this).create();
 NetAlert.setMessage("No Stream Found!");
        NetAlert.setButton("OK",
        newDialogInterface.OnClickListener() {
        publicvoidonClick(
    DialogInterface dialog,int which) {
        // here you can add functions// finish();
                }
                });
                NetAlert.show();
                returntrue;
            }
        });

        vid.setOnPreparedListener(newOnPreparedListener() {
            @OverridepublicvoidonPrepared(MediaPlayer mp) {

                videoBuffering.cancel();
            }
        });

    }
} else {
    Toast.makeText(getBaseContext(), "No Internet Connection",
            Toast.LENGTH_SHORT).show();
    // Toast t =// Toast.makeText(this,"You are not online!!!!",8000).show();
    Log.v("Home",
        "############################You are not online!!!!");
}
}
            returnnull;
        }

        protectedvoidonPostExecute(String file_url) {
            pDialog.dismiss();
        }
    }

here pDialog is your ProgressDialog and you can define it like this ProgressDialog pDialog; Or if you don't want to fore this event on button;s click event then you can directly use this line in your onCreate method new asyn().execute();.

Edit

I have edited my answer, but you have to make some necessary changes in this, i think it will ask you add try and catch block somewhere, then just add it.

Solution 2:

Go through this link to learn AsyncTask

and try to write your logic in doInBackground method of AsyncTask

Solution 3:

splashHandler.sendEmptyMessageDelayed(0, 3000);


Handler splashHandler= new Handler()
{
    publicvoidhandleMessage(android.os.Message msg) 
    {
        switch (msg.what) 
        {
        case0:

            //Your code is here....break;
            default:
                break;
        }
    }
};

Post a Comment for "How To Run This Code In A Thread Or Async Task?"