Java Android可运行线程被卡住了
我的活动是将以下onCreate()与可运行线程一起使用while循环。代码如下:Java Android可运行线程被卡住了,java,android,multithreading,Java,Android,Multithreading,我的活动是将以下onCreate()与可运行线程一起使用while循环。代码如下: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_post_user_registration); SharedPreferences sp = PreferenceMan
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_user_registration);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sp.edit();
//Some Async task here
final File destFile = new File(filesDir, sp.getString(Constants.SharedPreferences.USER_ID,null)+ Constants.S3Bucket.IMAGE_EXTENSION);
final File downFile = new File(filesDir, Constants.S3Bucket.USER_DIRECTORY + sp.getString(Constants.SharedPreferences.USER_ID,null)+ Constants.S3Bucket.IMAGE_EXTENSION);
Runnable myRunnable = new Runnable() {
@Override
public void run() {
Log.v(LOG_TAG,"S3 file profile exists "+downFile.getAbsolutePath()+" "+downFile.exists());
while(!downFile.exists()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e(LOG_TAG,"Error",e);
}
Log.v(LOG_TAG,"Inside while loop");
}
Log.v(LOG_TAG,"S3 file profile exists after while "+downFile.getAbsolutePath()+" "+downFile.exists());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
};
Thread myThread = new Thread(myRunnable);
myThread.start();
}
线程基本上检查由AWS服务创建的下载文件。我的活动保持运行,控件不进入while循环。该条件第一次为真,之后控件既不进入while循环也不退出。while循环之前的语句已正确记录。所以,基本上主活动并没有开始
这是线程的错误实现吗?我不明白这里发生了什么
如果我停止应用程序并再次运行它,则条件现在为false,因此控件不会进入while循环,但下一个活动已正确加载。使用AsyncTask完成您正在执行的操作,您不必陷入无休止的while循环 以下是使用它们的完整说明: 我在这里为服务器上的grabbinf图像实现了一个:(下面的代码片段) 甚至安卓在这里也有使用它们的教程: 上一个链接中的示例:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
我自己使用的AsynTask
public class GetImageFromServer extends AsyncTask<Void, Void, Bitmap> {
private final String tag = "WLGFX-AsyncBitmap";
private Context context;
private ImageView imageView;
private int width;
private int height;
GetImageFromServer(Context ctx, ImageView view) {
context = ctx;
imageView = view;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
imageView.setImageBitmap(null);
width = imageView.getWidth();
height = imageView.getHeight();
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
Log.d(tag, "Bitmap: " +
"(" + bitmap.getWidth() +
"," + bitmap.getHeight() + ")" +
" " + bitmap.getByteCount());
}
@Override
protected Bitmap doInBackground(Void... voids) {
String head = "GSIF";
byte[] header_data = head.getBytes();
try {
InetAddress address = InetAddress.getByName(Global.server_host);
Socket socket = new Socket(address, Global.tcp_port);
OutputStream out = socket.getOutputStream();
out.write(header_data);
byte[] dim = new byte[2];
dim[0] = (byte)(width >> 8);
dim[1] = (byte)width;
out.write(dim);
dim[0] = (byte)(height >> 8);
dim[1] = (byte)(height);
out.write(dim);
byte[] length = new byte[4];
readSocketBytes(socket, length);
int size = (length[0] & 255) << 24 |
(length[1] & 255) << 16 |
(length[2] & 255)<< 8 |
length[3] & 255;
byte[] image_data = new byte[size];
readSocketBytes(socket, image_data);
Bitmap bitmap = BitmapFactory.decodeByteArray(image_data, 0, size);
socket.close();
return bitmap;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void readSocketBytes(Socket socket, byte[] data) throws IOException {
int length = data.length;
InputStream in = socket.getInputStream();
int position = 0;
while (position < length) {
int count = in.read(data, position, length - position);
if (count > 0) position += count;
else throw new IOException();
}
}
}
公共类GetImageFromServer扩展异步任务{
私有最终字符串tag=“WLGFX AsyncBitmap”;
私人语境;
私人影像视图;
私有整数宽度;
私人内部高度;
GetImageFromServer(上下文ctx、ImageView){
上下文=ctx;
图像视图=视图;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
setImageBitmap(空);
width=imageView.getWidth();
height=imageView.getHeight();
}
@凌驾
受保护的void onPostExecute(位图){
onPostExecute(位图);
设置图像位图(位图);
Log.d(标记“位图:”+
(“+bitmap.getWidth()+
“,“+bitmap.getHeight()+””+
“”+bitmap.getByteCount());
}
@凌驾
受保护位图doInBackground(无效…无效){
字符串头=“GSIF”;
byte[]header_data=head.getBytes();
试一试{
InetAddress=InetAddress.getByName(全局.server\u主机);
套接字=新套接字(地址,全局.tcp\u端口);
OutputStream out=socket.getOutputStream();
out.write(表头\ U数据);
字节[]dim=新字节[2];
尺寸[0]=(字节)(宽度>>8);
dim[1]=(字节)宽度;
输出。写入(dim);
尺寸[0]=(字节)(高度>>8);
尺寸[1]=(字节)(高度);
输出。写入(dim);
字节[]长度=新字节[4];
readSocketBytes(套接字,长度);
int size=(长度[0]&255)使用异步任务完成您正在做的事情,您不必陷入无休止的while循环
以下是使用它们的完整说明:
我在这里为服务器上的grabbinf图像实现了一个:(下面的代码片段)
甚至安卓在这里也有使用它们的教程:
上一个链接中的示例:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
我自己使用的AsynTask
public class GetImageFromServer extends AsyncTask<Void, Void, Bitmap> {
private final String tag = "WLGFX-AsyncBitmap";
private Context context;
private ImageView imageView;
private int width;
private int height;
GetImageFromServer(Context ctx, ImageView view) {
context = ctx;
imageView = view;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
imageView.setImageBitmap(null);
width = imageView.getWidth();
height = imageView.getHeight();
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
Log.d(tag, "Bitmap: " +
"(" + bitmap.getWidth() +
"," + bitmap.getHeight() + ")" +
" " + bitmap.getByteCount());
}
@Override
protected Bitmap doInBackground(Void... voids) {
String head = "GSIF";
byte[] header_data = head.getBytes();
try {
InetAddress address = InetAddress.getByName(Global.server_host);
Socket socket = new Socket(address, Global.tcp_port);
OutputStream out = socket.getOutputStream();
out.write(header_data);
byte[] dim = new byte[2];
dim[0] = (byte)(width >> 8);
dim[1] = (byte)width;
out.write(dim);
dim[0] = (byte)(height >> 8);
dim[1] = (byte)(height);
out.write(dim);
byte[] length = new byte[4];
readSocketBytes(socket, length);
int size = (length[0] & 255) << 24 |
(length[1] & 255) << 16 |
(length[2] & 255)<< 8 |
length[3] & 255;
byte[] image_data = new byte[size];
readSocketBytes(socket, image_data);
Bitmap bitmap = BitmapFactory.decodeByteArray(image_data, 0, size);
socket.close();
return bitmap;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void readSocketBytes(Socket socket, byte[] data) throws IOException {
int length = data.length;
InputStream in = socket.getInputStream();
int position = 0;
while (position < length) {
int count = in.read(data, position, length - position);
if (count > 0) position += count;
else throw new IOException();
}
}
}
公共类GetImageFromServer扩展异步任务{
私有最终字符串tag=“WLGFX AsyncBitmap”;
私人语境;
私人影像视图;
私有整数宽度;
私人内部高度;
GetImageFromServer(上下文ctx、ImageView){
上下文=ctx;
图像视图=视图;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
setImageBitmap(空);
width=imageView.getWidth();
height=imageView.getHeight();
}
@凌驾
受保护的void onPostExecute(位图){
onPostExecute(位图);
设置图像位图(位图);
Log.d(标记“位图:”+
(“+bitmap.getWidth()+
“,“+bitmap.getHeight()+””+
“”+bitmap.getByteCount());
}
@凌驾
受保护位图doInBackground(无效…无效){
字符串头=“GSIF”;
byte[]header_data=head.getBytes();
试一试{
InetAddress=InetAddress.getByName(全局.server\u主机);
套接字=新套接字(地址,全局.tcp\u端口);
OutputStream out=socket.getOutputStream();
out.write(表头\ U数据);
字节[]dim=新字节[2];
尺寸[0]=(字节)(宽度>>8);
dim[1]=(字节)宽度;
输出。写入(dim);
尺寸[0]=(字节)(高度>>8);
尺寸[1]=(字节)(高度);
输出。写入(dim);
字节[]长度=新字节[4];
readSocketBytes(so)