- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 Android 新手,并不知道如何处理这个问题:我有一个 AsyncTask,它从 XML 文件中读取位置 (X,Y,Z)。由于这个位置每秒都在变化,我希望在我按下一个按钮(用“StartListener”调用)后连续读取和绘制每个新位置并在我再次按下按钮时停止读取它......
有人可以帮助我吗? - 这是我的 MainActivity 的一部分
(目前我的应用程序仅在我按下按钮时读取和绘制位置...)
private OnClickListener StartListener = new OnClickListener() {
@Override
public void onClick(View v) {
TextView ButText = (TextView)findViewById(R.id.buttonStart);
String value=ButText.getText().toString();
if(value.equals("Start positioning")){
ButText.setText("Stop positioning");
new PositionAsync().execute(); //read data from XML file
}
else if(value.equals("Stop positioning")){
ButText.setText("Start positioning");
//new PositionAsync().cancel(true);
}
}
}; // END LISTENER START BUTTON
// READ XML FILE
class PositionAsync extends AsyncTask<Void, Void, Void> {
XMLHelper helper;
@Override
protected Void doInBackground(Void... arg0) {
helper = new XMLHelper();
helper.get();
return null;
}
@Override
protected void onPostExecute(Void result) {
Paint paintBlack = new Paint(); paintBlack.setAntiAlias(true); paintBlack.setColor(Color.BLACK);
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
File ImageSource = new File("/sdcard/app_background3.jpg");
Bitmap bitmap2 = BitmapFactory.decodeFile(ImageSource.getAbsolutePath(),myOptions);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap2);
Bitmap mutableBitmap2 = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas2 = new Canvas(mutableBitmap2);
float RoomWidthPx = canvas2.getWidth();
float RoomHeightPx = canvas2.getHeight();
float RoomXmeter = (float) 9.3;
float RoomZmeter = (float) 14.7;
for (PositionValue position : helper.positions) {
String PosX = position.getPositionX(); String PosY = position.getPositionY(); String PosZ = position.getPositionZ();
float x = Float.valueOf(PosX); float y = Float.valueOf(PosY); float z = Float.valueOf(PosZ);
float xm = x*RoomWidthPx/RoomXmeter;
float zm = z*RoomHeightPx/RoomZmeter;
canvas2.drawCircle(xm, zm, 25, paintBlack);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap2);
// SAVE DRAWINGS INTO FILE
FileOutputStream fos = null;
try {
fos = new FileOutputStream ("/sdcard/app_background3.jpg");
mutableBitmap2.compress (Bitmap.CompressFormat.JPEG, 95, fos);
} catch (Throwable ex) {ex.printStackTrace (); }
};
}
} //END READ XML FILE
最佳答案
我认为你在一秒钟内完成了太多任务。相反,您可以在 AsyncTask 的 onPreExecute()
中准备所有繁重的工作人员,在 doInBackground()
中读取 XML 并进行绘制,在onProgressUpdate()
最后,当任务完成时,将图像保存到 sdcard
。
我已经修改了您的 Asynctask
来完成上述场景,我没有测试它,但它给了您想法。
在 Activity 的 onCreate()
方法中,您只需启动 AsyncTask 一次。它会一直执行或休眠,直到您将 Quit_Task 变量设置为 true。当按下按钮时,您切换变量 Do_Drawing: Do_Drawing=!Do_Drawing;
就这样。
private boolean Do_Drawing = false;
private boolean Quit_Task = false;
// READ XML FILE
class PositionAsync extends AsyncTask<Void, Void, Void>
{
Paint paintBlack;
BitmapFactory.Options myOptions;
Bitmap mutableBitmap2;
Canvas canvas2;
XMLHelper helper;
void Sleep(int ms)
{
try
{
Thread.sleep(ms);
}
catch (Exception e)
{
}
}
@Override
protected void onPreExecute()
{
// Prepare everything for doInBackground thread
paintBlack = new Paint();
paintBlack.setAntiAlias(true);
paintBlack.setColor(Color.BLACK);
myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
File ImageSource = new File("/sdcard/app_background3.jpg");
Bitmap bitmap2 = BitmapFactory.decodeFile(ImageSource.getAbsolutePath(), myOptions);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap2);
mutableBitmap2 = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
canvas2 = new Canvas(mutableBitmap2);
helper = new XMLHelper();
}
@Override
protected Void doInBackground(Void... arg0)
{
while (!Quit_Task)
{
// Sleep until button is pressed or quit
while (!Do_Drawing)
{
Sleep(1000);
if (Quit_Task)
return null;
}
float RoomWidthPx = canvas2.getWidth();
float RoomHeightPx = canvas2.getHeight();
float RoomXmeter = (float) 9.3;
float RoomZmeter = (float) 14.7;
// keep drawing until button is pressed again or quit
while (Do_Drawing)
{
if (Quit_Task)
return null;
helper.get();
for (PositionValue position : helper.positions)
{
String PosX = position.getPositionX();
String PosY = position.getPositionY();
String PosZ = position.getPositionZ();
float x = Float.valueOf(PosX);
float y = Float.valueOf(PosY);
float z = Float.valueOf(PosZ);
float xm = x * RoomWidthPx / RoomXmeter;
float zm = z * RoomHeightPx / RoomZmeter;
canvas2.drawCircle(xm, zm, 25, paintBlack);
}
this.publishProgress((Void) null);
Sleep(1000);
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... progress)
{
// once all points are read & drawn refresh the imageview
try
{
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap2);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Void result)
{
// SAVE DRAWINGS INTO FILE once the task is done.
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "app_background3.jpg");
mutableBitmap2.compress(Bitmap.CompressFormat.JPEG, 95, fos);
}
catch (Throwable ex)
{
ex.printStackTrace();
}
}
} // END READ XML FILE
关于Android 如何每秒运行一个 AsyncTask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18702298/
我今天在我的 Android 项目中遇到了一个涉及 aSyncTasks 的问题,经过一些研究找到了答案,并且与我交谈过的人都没有意识到,所以我想我会与 SO 社区分享,以防万一有人发现它有任何用处。
我目前正在 AsyncTask 的 onPostExecute 方法中执行类似的操作,其中 NewTask 不是当前正在执行的任务: private class OlderTask extends A
所以,我正在研究一个条形码解码器,一旦我们有了条形码,它就会通过互联网进入多个 API 来解码刚刚扫描的内容。问题是我必须将一些 XML 解析链接在一起,我不知道我做的是否正确。 因此,扫描条形码后,
我的团队开发了一个新的 Android 应用程序,它广泛使用了 Room。 我不确定我们是否正确使用 AsyncTask。 我们不得不在 AsyncTasks 中包装所有对 insert/update
我查看了其他问题,但未能澄清我对从另一个任务调用任务的疑问,我有以下代码: protected List doInBackground(String... params) try {
我正在开发一个访问 Web 服务的应用程序,并使用从中获取的 JSON 创建一个对象并在我的代码中使用它。尽管我的应用程序正在运行,但我不知道它是否写得很好且完美无缺。 我将解释我的内容,然后放置一些
虽然我还没有尝试过,但从理论上讲,我问这个问题只是为了消除我的疑虑。 我有这样一个场景:1. 向服务器发送请求并接收 JSON 响应。为此,我正在使用 AsyncTask,因为接收响应可能会有延迟。2
我有以下 AsyncTask 的实现,允许多个 AsyncTask 同时运行: public abstract class MyAsyncTask extends AsyncTask { pu
花了很多时间试图解决这个问题,我已经阅读了很多问题、论坛、答案......但它仍然不会更新 UI。 我的最终目标是从用户那里获取一个搜索词,并将一个 httprequest 发送到用 JSON 回复的
我有一个异步任务 private class LoadData extends AsyncTask { private String WEBURL; LoadData(String u
我正在用 android 做一些编码工作。我几乎遇到了一个问题,为了解决这个问题,我需要一个匿名 AsyncTask 类来执行。但我还需要在执行之前传递并反对这个类。我尝试了下面的代码,但它不起作用,
我有两个 AsyncTask:第一个寻找素数,如果成功,我必须调用第二个 AsyncTask 显示单词“Yop!” (将这个词添加到数组列表中,并显示在AsyncTask三)。 如果我从 onProg
我想使用 AsyncTask 将图像加载到 ListView。 private class LoadImageTask extends AsyncTask,Void,Bitmap>{ @Sup
在我的 AsyncTask 的某个时刻,在完成一些验证之后,我需要派生另一个线程来做一些其他工作。所以我现在想要两个后台线程,每个都做自己的事情(每个执行大约 2-3 秒)。我们的想法是最大限度地提高
(这与空指针无关):我在 AsyncTask 中有一个进度条,并且添加了一个取消按钮来取消 asynctask。 我可以从异步任务外部取消异步任务,但我需要在异步任务下实现的progressdialo
我有一个 Activity ,在启动时调用“json”来获取歌曲的数据类别,之后我调用方法“AsyncTask”来获取来自另一个“JSON”的歌曲列表问题是,当我启动 Activity 时,它被锁定,
我想做以下事情。我想显示一个包含信息和图像的列表。这些图像需要一段时间才能加载,所以我想我会采取不同的方式。我会使用两个 AsyncTasks。第一个创建所有布局并用除图像之外的数据填充它。第二个只是
如果我做了这样的事情: public class MyFragment extends Fragment { private GetDataTask mGDT; //onCreat
在 Android Activity 中,我在 onCreate 方法中执行 AsyncTask。我应该在 AsyncTask 的 onPostExecute 中还是在 OnCreate 方法中声明
我对 AsyncTask 有疑问我尝试为 10 个 Json 文件向互联网打开 10 个请求,因此我读取它并将其保存到用户设备_由于数据差异,此文件必须分开,包括。 那么,将每个请求放在单个 Asyn
我是一名优秀的程序员,十分优秀!