问题描述
我想使用非阻止IO读取背景过程中的流/输出.有人可以给我一个有关如何在Android上使用非阻止IO的示例吗?
感谢您的帮助.
推荐答案
这是我使用的类从Internet下载文件或复制文件系统中的文件以及我如何使用它:
// 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);
还有选择器方法,这里有一些关于选择器,频道和线程的很棒的(Java)教程:
- http://jfarcand.wordpress.com/2006/05/05/30/tricks-and-with-with-nio-nio-part-i------------------------------------ li>
- http://jfarcand.wordpress.com/2006/07/06/tricks-and-tricks-and-with-with-nio-part-ii--i-why-selectionkey-selectionkey-attach-is-is-evil/
- http://jfarcand.wordpress.com/2006/07/07/tricks-and-pricks-and-with-with-nio-part-iii-part-iii-to-thread-thread-thread-or-thread-or-not-not-not-not-not-not-to-thread/
- http://jfarcand.wordpress.com/2006/07/19/httpweblogs-java-netblog2006071919tricks-and-and-tips-and-tips-nio-part-part-iv-meet-selectors/
- http://jfarcand.wordpress.com/2006/09/21/tricks-and-pricks-and-with-with-nio-part-nio-part-vart-v-ssl-v-ssl-nio-nio-friend-oriend-or-friend-or-foe/-
其他推荐答案
背景操作可以在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开发人员站点中找到文件访问的参考.
问题描述
I want to use an Non Blocking IO to read a stream/output from a background process.Can anyone give me an example on how to use the Non Blocking IO on Android?
Thanks for the help.
推荐答案
Here is the class I use to download a file from the internet or copy a file within the filesystem 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 also is the Selector approach, here are some great (Java) tutorials about Selectors, Channels and Threads:
- http://jfarcand.wordpress.com/2006/05/30/tricks-and-tips-with-nio-part-i-why-you-must-handle-op_write
- http://jfarcand.wordpress.com/2006/07/06/tricks-and-tips-with-nio-part-ii-why-selectionkey-attach-is-evil/
- http://jfarcand.wordpress.com/2006/07/07/tricks-and-tips-with-nio-part-iii-to-thread-or-not-to-thread/
- http://jfarcand.wordpress.com/2006/07/19/httpweblogs-java-netblog20060719tricks-and-tips-nio-part-iv-meet-selectors/
- http://jfarcand.wordpress.com/2006/09/21/tricks-and-tips-with-nio-part-v-ssl-and-nio-friend-or-foe/
其他推荐答案
background operation can be done in many ways in Android. I suggest you to use 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 } }
Then you execute it like this:
public void onClick(View v) { new LongOperation().execute(""); }
reference: xoriant.com
In the doInBackground method, you can put your file access stuff. reference for file access can be found in the android developer site.