Android: Unbuffered IO

I want to use Non Blocking IO to read thread / output from a background process. Can someone give me an example of how to use Non Blocking IO on Android?

Thanks for the help.

+1
source share
2 answers

Here is the class that I use to download a file from the Internet or copy a file to the file system and how I use it:

// Download a file to /data/data/your.app/files
new DownloadFile(ctxt, "http://yourfile", ctxt.openFileOutput("destinationfile.ext", Context.MODE_PRIVATE));

// Copy a file from raw resource to the files directory as above
InputStream in = ctxt.getResources().openRawResource(R.raw.myfile);
OutputStream out = ctxt.openFileOutput("filename.ext", Context.MODE_PRIVATE);
final ReadableByteChannel ic = Channels.newChannel(in);
final WritableByteChannel oc = Channels.newChannel(out);
DownloadFile.fastChannelCopy(ic, oc);

There is also a Selector approach, here are some excellent (Java) tutorials on selectors, channels, and streams:

+1

Android. AsyncTask:

private class LongOperation extends AsyncTask<String, Void, String> {

 @Override
 protected String doInBackground(String... params) {
  // perform long running operation operation
  return null;
 }

 /* (non-Javadoc)
  * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
  */
 @Override
 protected void onPostExecute(String result) {
  // execution of result of Long time consuming operation
 }

 /* (non-Javadoc)
  * @see android.os.AsyncTask#onPreExecute()
  */
 @Override
 protected void onPreExecute() {
 // Things to be done before execution of long running operation. For example showing ProgessDialog
 }

 /* (non-Javadoc)
  * @see android.os.AsyncTask#onProgressUpdate(Progress[])
  */
 @Override
 protected void onProgressUpdate(Void... values) {
      // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
  }
}

:

public void onClick(View v) {
   new LongOperation().execute("");
}

: xoriant.com

doInBackground . Android.

+1

Source: https://habr.com/ru/post/1777219/


All Articles