HandlerThread 的 handleMessage() 没有被调用
HandlerThread's handleMessage() is not called
我刚刚在使用 HandlerThread 和 Handlers 时遇到了一个奇怪的问题(我所说的 Handlers 是指 android.os.handler)。我正在尝试将消息从我的片段传递到单独的处理程序线程。我只是设置了一个 handleMessage 来处理消息。但它没有被调用。在使用 start() 启动线程后(在循环程序准备好之后),我已经获得了处理程序线程的循环程序。我还检查了处理程序是否使用 hasMessage() 获得了消息。一切都好。我很迷惑。请帮助 me.My 代码如下。
片段代码:
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();
}
}
}
我的问题的答案:
我可以发送消息,也可以从其他线程接收消息到我的 handleMessage()。我已经扩展了 HandlerThread class 并为我的线程 class 实现了 Handler.Callback。所以我只是使用下面的版本来实例化一个 Handler 将线程作为回调传递(因为它实现了 Handler.Callback 它被接受为回调)。我希望它有所帮助。如果您感到困惑,请仔细阅读代码。谢谢。
Handler myHandler = new Handler(myThread.getLooper(),myThread);
我刚刚在使用 HandlerThread 和 Handlers 时遇到了一个奇怪的问题(我所说的 Handlers 是指 android.os.handler)。我正在尝试将消息从我的片段传递到单独的处理程序线程。我只是设置了一个 handleMessage 来处理消息。但它没有被调用。在使用 start() 启动线程后(在循环程序准备好之后),我已经获得了处理程序线程的循环程序。我还检查了处理程序是否使用 hasMessage() 获得了消息。一切都好。我很迷惑。请帮助 me.My 代码如下。
片段代码:
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();
}
}
}
我的问题的答案:
我可以发送消息,也可以从其他线程接收消息到我的 handleMessage()。我已经扩展了 HandlerThread class 并为我的线程 class 实现了 Handler.Callback。所以我只是使用下面的版本来实例化一个 Handler 将线程作为回调传递(因为它实现了 Handler.Callback 它被接受为回调)。我希望它有所帮助。如果您感到困惑,请仔细阅读代码。谢谢。
Handler myHandler = new Handler(myThread.getLooper(),myThread);