我的应用奇怪地在无限循环中疯狂运行。这是下载图像并用作墙纸的简单代码。
但是当删除此行代码时,循环消失了,也就是说,AsyncTask
恢复了预期的工作:
WallpaperManager.getInstance(weakContext.get()).setBitmap(bitmap);
我已经认为问题可能是WeakReference
,但是即使通过使用onPostExecute()
在Listener
方法内部运行代码来执行Fragment
内的更改问题仍然存在。
public class WorkerTask extends AsyncTask<String, Void, Bitmap> {
private static final String TAG = "WorkerTask";
private WeakReference<Context> weakContext;
private WeakReference<ImageView> weakImage;
private Bitmap wallpaper;
private WallpaperListener wallpaperListener;
WorkerTask(Context context, ImageView imageView, WallpaperListener listener){
weakContext = new WeakReference<>(context);
weakImage = new WeakReference<>(imageView);
wallpaperListener = listener;
}
@Override
protected Bitmap doInBackground(String... url) {
Log.d(TAG, "doInBackground: ");
if (url[0].isEmpty()){
throw new NullPointerException("URL Bad formatted");
}
/* Network */
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url[0])
.build();
Response response = null;
try {
response = client.newCall(request).execute();
/* Generate Image */
wallpaper = BitmapFactory.decodeStream(
Objects.requireNonNull(response.body()).byteStream());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
response.close();
}
}
return wallpaper;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
try {
WallpaperManager.getInstance(weakContext.get()).setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class WorkerFragment extends Fragment {
private static final String TAG = "WorkerFragment";
private ImageView imageView;
private TextView textView;
private Bitmap wallpaper;
// String UrlApi = "https://images.unsplash.com/photo-1511736515797-8aab81ec7e35?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=966f3c728ebb930b49192fdbf14b55e0&auto=format&fit=crop&w=1534&q=80";
String UrlApi = "https://images.unsplash.com/photo-1535632788826-78ca9d09d2e7?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=bb651ad5967f2074c98b8c30ae2fc442&auto=format&fit=crop&w=695&q=80";
public WorkerFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_worker, container, false);
imageView = rootView.findViewById(R.id.imageView);
TextView textView = rootView.findViewById(R.id.textView);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated: ");
final Runnable setImage = new Runnable() {
@Override
public void run() {
Log.d(TAG, "run2: ");
imageView.setImageBitmap(wallpaper);
setWallpaper(wallpaper);
}
};
Runnable getImage = new Runnable() {
@Override
public void run() {
Log.d(TAG, "run1: ");
wallpaper = getWallpaper();
imageView.post(setImage);
}
};
Thread workerThread = new Thread(getImage);
workerThread.start();
}
private Bitmap getWallpaper(){
Request request = new Request.Builder()
.url(UrlApi)
.build();
OkHttpClient client = new OkHttpClient();
Bitmap bitmap = null;
try {
Response response = client.newCall(request).execute();
bitmap = BitmapFactory.decodeStream(response.body().byteStream());
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
private void setWallpaper(Bitmap bitmap){
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity().getApplicationContext());
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Logcat:
答案 0 :(得分:0)
我发现了使Thread循环无限的原因,但不是原因。简而言之,这是 LineageOS 15.1功能,名为StyleInterface,用于更改系统的颜色以匹配墙纸或时间。设置为匹配墙纸时,会发生错误。
我正在使用自定义ROM在植根设备上进行测试。在普通rom设备上不会发生这种情况。