我想在doInBackground中显示toast消息,如下所示。
private class myAsync extends AsyncTask<Void, Integer, Void>
{
int duration = 0;
int current = 0;
int inc = 0;
@Override
protected Void doInBackground(Void... params) {
videoView.start();
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
duration = videoView.getDuration();
}
});
do {
current = videoView.getCurrentPosition();
inc = inc + 1;
if(inc == 10000 || inc == 25000 || inc == 30000){
Toast.makeText(getApplicationContext(), inc, Toast.LENGTH_LONG).show();
}
} while (current <= duration);
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Toast toast = Toast.makeText(getApplicationContext(),values[0], Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
我希望在10000,25000,30000时显示inc值。
我希望知道这个的人帮助我。
由于
答案 0 :(得分:1)
假设MyAsync嵌套在一个Activity中:
private class myAsync extends AsyncTask<Void, Integer, Void>
{
int duration = 0;
int current = 0;
int inc = 0;
@Override
protected Void doInBackground(Void... params) {
videoView.start();
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
duration = videoView.getDuration();
}
});
do {
current = videoView.getCurrentPosition();
inc = inc + 1;
if(inc == 10000 || inc == 25000 || inc == 30000){
showToast(inc);
}
} while (current <= duration);
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
showToast(values[0]);
// Todo integrate gravity parameter
}
private void showToast(String text)
{
MainActivity.this.runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
}
});
}
}
答案 1 :(得分:0)
我把它解决了,因为我把publishProgress(inc);在if语句中
if(inc == 10000 || inc == 25000 || inc == 30000){
publishProgress(inc);
}
它有效。