Java 如何从onProgressUpdate和onPostExecute获取响应
我有一个活动和一个任务。我希望异步任务中的进度结果显示在活动文本视图中,最终结果显示在活动文本视图中。我陷入了最后一点,即如何从Java 如何从onProgressUpdate和onPostExecute获取响应,java,android,android-asynctask,Java,Android,Android Asynctask,我有一个活动和一个任务。我希望异步任务中的进度结果显示在活动文本视图中,最终结果显示在活动文本视图中。我陷入了最后一点,即如何从onProgressUpdate()和onPostExecute()发送结果。帮我解决这个问题 MainActivity.java public class MainActivity extends Activity { TextView tv; Button b1; AsyncTask<Integer, Integer, Integer> ta; pri
onProgressUpdate()
和onPostExecute()发送结果。帮我解决这个问题
MainActivity.java
public class MainActivity extends Activity {
TextView tv;
Button b1;
AsyncTask<Integer, Integer, Integer> ta;
private static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.viewer);
b1 = (Button) findViewById(R.id.clicker);
tv.setText("2");
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int val = Integer.parseInt(tv.getText().toString());
ta = new TestAsync(getApplicationContext()).execute(val);
}
});
}
public void callOnEnd() {
try {
Log.d(TAG, "TestAsync " + ta.get() + " status " + ta.getStatus());
} catch(Exception e) {
Log.d(TAG, "TestAsync Exception " + e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public class TestAsync extends AsyncTask<Integer, Integer, Integer>{
Context context;
private static String TAG = "TestAsync";
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
public TestAsync(Context ctx) {
this.context = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(TAG, TAG + "Reached AsyncTask");
}
@Override
protected Integer doInBackground(Integer... params) {
Log.d(TAG, TAG + "doInBackground");
int val = params[0], i, res;
for(i=0; i<100; i++) {
res = i * val;
publishProgress(res);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.d(TAG, TAG + " onProgressUpdate " + values[0]);
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
Log.d(TAG, TAG + "onPostExecute " + result);
}
}
公共类MainActivity扩展活动{
文本视图电视;
按钮b1;
异步任务ta;
私有静态字符串TAG=“MainActivity”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.viewer);
b1=(按钮)findViewById(R.id.clicker);
tv.setText(“2”);
b1.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
int val=Integer.parseInt(tv.getText().toString());
ta=NewTestAsync(getApplicationContext()).execute(val);
}
});
}
public void callOnEnd(){
试一试{
Log.d(标记“TestAsync”+ta.get()+“status”+ta.getStatus());
}捕获(例外e){
Log.d(标记“TestAsync异常”+e.toString());
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
}
TestAsync.java
public class MainActivity extends Activity {
TextView tv;
Button b1;
AsyncTask<Integer, Integer, Integer> ta;
private static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.viewer);
b1 = (Button) findViewById(R.id.clicker);
tv.setText("2");
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int val = Integer.parseInt(tv.getText().toString());
ta = new TestAsync(getApplicationContext()).execute(val);
}
});
}
public void callOnEnd() {
try {
Log.d(TAG, "TestAsync " + ta.get() + " status " + ta.getStatus());
} catch(Exception e) {
Log.d(TAG, "TestAsync Exception " + e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public class TestAsync extends AsyncTask<Integer, Integer, Integer>{
Context context;
private static String TAG = "TestAsync";
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
public TestAsync(Context ctx) {
this.context = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(TAG, TAG + "Reached AsyncTask");
}
@Override
protected Integer doInBackground(Integer... params) {
Log.d(TAG, TAG + "doInBackground");
int val = params[0], i, res;
for(i=0; i<100; i++) {
res = i * val;
publishProgress(res);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.d(TAG, TAG + " onProgressUpdate " + values[0]);
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
Log.d(TAG, TAG + "onPostExecute " + result);
}
}
公共类TestAsync扩展异步任务{
语境;
私有静态字符串TAG=“TestAsync”;
SimpleDataFormat sdf=新SimpleDataFormat(“mm:ss”);
公共测试同步(上下文ctx){
this.context=ctx;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Log.d(标记,标记+“任务”);
}
@凌驾
受保护的整数doInBackground(整数…参数){
Log.d(TAG,TAG+“doInBackground”);
int val=参数[0],i,res;
对于(i=0;i您在onProgressUpdate和onPostUpdate中所做的任何操作都将直接发布到UIThread。只需将您的逻辑写入这两个函数中,您就可以访问这两个函数中UI线程的任何组件。有两种可能性:
将AsyncTask
实现为一个内部类:
您可以从onProgressUpdate()
或onPostUpdate()
中访问MainActivity。您可以调用类似这样的东西
@Override
protected void onPostExecute(Integer result) {
MainActivity.this.tv.setText("my result is: " + result);
}
您可以在MainActivity
中创建某种类型的接口,将其传递给AsyncTask
,以便从这些方法中调用它
public class MainActivity extends Activity {
// all your code
public void onResult(Integer result) {
tv.setText("my result is: " + result);
}
}
在您的异步任务中
:
@Override
protected void onPostExecute(Integer result) {
((MainActivity) context).onResult(result);
}
您需要使用main活动创建TestAsync()
。这是而不是getApplicationContext()
。否则,强制转换将失败
一个合适的界面
会更好,但你知道了
你能给我提供1个代码在onProgressUpdate()中显示结果吗到活动中的TextView。在onProgessUpdate中直接在TextView中写入值。两者都在2个不同的类文件中。如何在异步任务中获取TextView的对象我已更新了答案。可能为了简单起见,您可以在同一个文件中写入异步任务。否则,可以使用它访问另一个类中的activity元素s实例都在2个不同的类文件中。那么我应该如何尝试从asynctask onPostExecute()访问textView呢请参阅我的第二个选项:在MainActivity
中创建一个方法,并从AsyncTask
调用它。很抱歉,我刚刚在答案中发现了一个副本+过去的错误。一秒钟前修复了它。不得不在底部添加其他注释,对由此带来的不便表示歉意。2个错误1.onResult()所需的返回类型2.onPostExecute中的ClassCastException((主要活动)上下文)。。。。