我在使用HandlerThread和Handlers时遇到了一个奇怪的问题(By Handlers我的意思是android.os.handler)。我试图将消息从我的片段传递给单独的处理程序线程。我只是设置一个handleMessage来处理消息。但它没有被称为。在使用start()启动线程之后(在准备好looper之后)我得到了处理程序线程的looper。我还检查了处理程序使用hasMessage()获取消息。一切都好。我很迷惑。请帮助我。我的代码在下面。
片段代码:
package com.example.raaja.handlerone;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by Raaja on 08-12-2015.
*/
public class LoaderFragment extends Fragment {
Button loaderButton;
LoaderThread loadThread;
Handler loaderThreadHandler;
Handler loaderHandler;
String[] queArray;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loaderHandler = new Handler();
loadThread = new LoaderThread("LoaderThread",loaderHandler);
queArray = new String[]{"http://www.paynekillers.com/downloads/screens/17_screen05.jpg",
"http://www.hdwallpapers.in/walls/jacob_frye_assassins_creed_syndicate-wide.jpg",
"http://www.percivalconstantine.com/wp-content/uploads/2015/03/Tomb-Raider-Lara-Croft-Summit-2013.jpg",
"http://orig00.deviantart.net/f904/f/2014/155/e/a/batman_arkham_knight_hd_wallpaper_1_by_rajivcr7-d7l19pt.jpg",
"http://static5.gamespot.com/uploads/original/536/5360430/2753139-15774024117_9026e0a43c_o.jpg"};
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View loaderView = inflater.inflate(R.layout.loader_layout, container, false);
this.loaderButton = (Button) loaderView.findViewById(R.id.proc_button);
return loaderView;
}
@Override
public void onResume() {
super.onResume();
loaderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadThread.start();
Looper loop = loadThread.requestLooper();
loaderThreadHandler = new Handler(loop);
Message msg = Message.obtain(loaderThreadHandler, 1, queArray);
msg.sendToTarget();
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
loadThread.quit();
}
}
HandlerThread
package com.example.raaja.handlerone;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Created by Raaja on 08-12-2015.
*/
public class LoaderThread extends HandlerThread implements Handler.Callback{
Handler imageHandler;
Map<Integer,String> queMap;
HandlerActivity activity;
public LoaderThread(String name, Handler handler) {
super(name);
imageHandler= handler;
Log.d("inside : ", name);
}
Looper requestLooper(){
Looper loop = this.getLooper();
Log.d("In Thread : ", String.valueOf(loop.getThread().getName()));
return loop.myLooper();
}
@Override
public boolean handleMessage(Message msg) {
String[] que = (String[]) msg.obj;
queMap = new HashMap<Integer,String>();
if(que != null){Log.d("",que[3]);}
for (int i=0;i<=4; i++){
String url = que[i];
queMap.put(i,url);
Log.d("1",url);
}
Random rand = new Random();
String link = queMap.get(rand.nextInt(5));
loadImages(link);
return true;
}
void loadImages(final String url){
Log.d("inside Load :","Inside Load");
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
final Bitmap image = BitmapFactory.decodeStream(connection.getInputStream());
imageHandler.post(new Runnable() {
@Override
public void run() {
activity.loadImage(image, url);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
回答我的问题:
我能够发送消息,我可以从另一个线程接收到来自handleMessage()的消息。我已经扩展了HandlerThread类并为我的Thread类实现了Handler.Callback。所以我只是使用下面的版本来实例化一个Handler,将线程作为Callback传递(因为它实现了Handler.Callback,它被接受为回调)。我希望它有所帮助。如果您感到困惑,请仔细阅读代码。谢谢。
Handler myHandler = new Handler(myThread.getLooper(),myThread);