public final class Intents { // public final static String LIST_PAGE_RECEIVED = "com.olsoft.list.page.received"; // public final static String IMAGE_RECEIVED = "com.olsoft.image.received "; … }
public class SampleApplication extends Application { private static SampleApplication mInstance; @Override public void onCreate() { super.onCreate(); mInstance = this; … } /*** * */ public static SampleApplication getInstance() { return mInstance; } /*** * * * @param action – */ public void sendBroadcastIntent(String action) { final Intent intent = new Intent(action); sendBroadcast(intent); } /*** * , */ public static void notifyListPageLoaded() { getInstance().sendBroadcast(new Intent(Intents. LIST_PAGE_RECEIVED)); } /*** * , */ public static void notifyImageLoaded() { getInstance().sendBroadcast(new Intent(Intents.IMAGE_RECEIVED)); } … }
@Override public void OnDownloadComplete(byte[] data) { … parseData(data); SampleApplication.notifyListPageLoaded(); … } @Override public void OnDownloadComplete(byte[] data) { … mImage = BitmapFactory.decodeByteArray(data, 0, data.length); SampleApplication.notifyListPageLoaded(); … }
/*** * */ @Override protected void onResume() { super.onResume(); IntentFilter f = new IntentFilter(); f.addAction(Intents.LIST_PAGE_RECEIVED); f.addAction(Intents.IMAGE_RECEIVED); registerReceiver(mNotificationReceiver, f); … } /*** * */ @Override protected void onPause() { unregisterReceiver(mNotificationReceiver); super.onPause(); } /*** * . UI */ private BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intents.IMAGE_RECEIVED.equalsIgnoreCase(action)) { mHandler.removeMessages(MESSAGE_IMAGE_LOADED); mHandler.sendEmptyMessageDelayed(MESSAGE_IMAGE_LOADED, 250); } else if (Intents. LIST_PAGE_RECEIVED.equalsIgnoreCase(action)) { mHandler.sendEmptyMessage(MESSAGE_NEXT_PAGE); } } };
private final static int MESSAGE_NEXT_PAGE = 1; private final static int MESSAGE_IMAGE_LOADED = 2; /** * . , UI thread */ private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_NEXT_PAGE: mAdapter.notifyDataSetChanged(); break; case MESSAGE_IMAGE_LOADED: mAdapter.notifyDataSetChanged(); break; default: break; } super.handleMessage(msg); } };
Source: https://habr.com/ru/post/130977/
All Articles