所以......我从一个菜单中启动了这个过程,我通过调用一个intent来开始它....基本的东西。但我正在打电话的活动是查询手机里面和手机外的所有图像文件。香港专业教育学院曾尝试在内部课程中使用AsyncTask
,但这只是我对如何正确理解的理解。我不确定什么回到哪里和什么。
因此,我的问题是,我可以创建一个新的线程,在意图的调用上启动并运行活动吗?听起来这比通过我的活动并尝试使AsyncTask
正常工作更容易解决。
我认为这看起来可能是
Thread t = new Thread(){
public void run(){
//start mark creation dialog view
Intent intentMarkCreation = new Intent(MixView.this, MarkCreation.class);
startActivityForResult(intentMarkCreation, 10);
}
};
t.start();
我知道android应该对常规线程表现得很奇怪但是我无法弄清楚如何使用AsyncTask
而且我找不到很多很好的深度示例。
如果有人能够通过一些例子或一个高级教程为我阐明这一点,那么我将非常努力。 :)
哦,这是我试图放入asyncTask
的一堆代码public class GraffMarkCreation extends AsyncTask {
/* ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fileChooser.setAdapter(adapter); */
private EditText nameMark;
private EditText describeMark;
private Spinner fileChooser;
private Button SaveMarkForLaterUpload;
private Button uploadMark;
private RadioGroup radioButtonLayout;
private ProgressBar uploadProgress;
private Gallery fileGalleryChooser;
private ImageButton goBackButton;
@Override
public void onCreate(Bundle savedInstancedState)
{
super.onCreate(savedInstancedState);
this.setContentView(R.layout.markcreationform);
this.describeMark = (describeMark);
findViewById(R.id.describeMark);
this.nameMark = (nameMark);
findViewById(R.id.nameMark);
this.fileChooser = (fileChooser);
findViewById(R.id.fileChooser);
this.SaveMarkForLaterUpload = (SaveMarkForLaterUpload);
findViewById(R.id.SaveMarkForLaterUpload);
this.uploadMark = (uploadMark);
findViewById(R.id.uploadMark);
this.radioButtonLayout = (radioButtonLayout);
findViewById(R.id.radioButtonLayout);
this.uploadProgress = (uploadProgress);
findViewById(R.id.uploadProgress);
this.fileGalleryChooser = (fileGalleryChooser);
findViewById(R.id.fileGalleryChooser);
this.goBackButton = (goBackButton);
findViewById(R.id.goBackButton);
}}
////////////create list of images to populate the spinner view with///////////////////////
/* class findImages extends AsyncTask<String[],String,Cursor>{
@Override
protected Cursor doInBackground(String[]... arg0) {
// TODO Auto-generated method stub
String[] projection = { MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA };
String selection = "";
String[] selectionArgs = null;
Cursor mImageExternalCursor = managedQuery
(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs,null);
Cursor mImageInternalCursor = managedQuery(MediaStore.Images.Media.INTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null);
mImageExternalCursor.getString(mImageExternalCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
mImageInternalCursor.getString(mImageExternalCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
return mImageInternalCursor;
}
}
findImages findThemImages = new findImages();
String[] params = null;
findThemImages.execute(params);
////////////////////////////////////////////////////////////////////////////////////////////
radioButtonLayout.setOnCheckedChangeListener(this); */
/////Hook up radio buttons///////////////////////////////////////////////
/*
@Override
public void onCheckedChanged(RadioGroup radioButtonLayout, int buttonId) {
// TODO Auto-generated method stub
switch (buttonId){
case 1:
///if image button selected spinner is populated with
///list of images to chose from
/* String[] projection = { MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA };
String selection = "";
String[] selectionArgs = null;
Cursor mImageExternalCursor = managedQuery
(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
Cursor mImageInternalCursor = managedQuery(MediaStore.Images.Media.INTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null);
mImageExternalCursor.getString(mImageExternalCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
mImageInternalCursor.getString(mImageExternalCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
*/
/*
break;
case 2:
///if audio button selected spinner is populated with
///list of audio files to chose from
break;
case 3:
///if video button selected spinner is populated with
///list of videos to chose from
break;
case 4:
///if text button selected spinner is not populated and
///user will be uploading only a basic mark with text only
break;
}
}
答案 0 :(得分:1)
据我所知,它必须在UI线程上运行Activity
。您最好的选择是使用AsyncTask
或IntentService
。由于您要收集要在用户界面中使用的资源,我建议使用AsyncTask
(阅读this })。
您是否在扩展Activity
的{{1}}内创建了内部课程?如果是这样,我会说你几乎就在那里。这样,您AsyncTask
便可轻松访问AsyncTask
中的View
。也许如果您发布代码,我可以让您知道它需要如何重构/修复。
答案 1 :(得分:1)
创建一个扩展AsyncTask的新类(外部公共或内部私有)&lt;参数,进展,结果&gt;其中&lt;&gt;中的值是对象类型。将长时间运行的代码放在名为public Result doInBackground(Params... params)
。
创建AsyncTask的新实例,设置所需的任何值(通过标准的setter方法)(传递主UI的上下文是一个好主意),然后调用instance.execute()
以运行doInbackground
方法。创建以下方法以显示和隐藏进度条:
ProgressDialog pdia;
protected void onPreExecute()
{
super.onPreExecute();
pdia = new ProgressDialog(context);
pdia.setMessage("Loading...");
pdia.show();
}
protected void onPostExecute(Result result)
{
super.onPostExecute(result);
if (context != null){
pdia.dismiss();
}
阅读加里关联的指南并查看我写的内容 - 它应该全部汇集在一起。