在活动数据已显示时刷新活动

时间:2017-10-23 18:17:49

标签: android android-activity

我公司生产制造设备。该设备由PC控制,PC通过工厂的WiFi与运行我们的应用程序(想想:智能手机)的Android设备进行通信。

大多数工业流程都与活动类及其屏幕相关联。工厂工人在&#34;电话&#34;上选择一个,Android向PC发送消息请求,并且PC发送命令和参数,用于启动该活动并使用intent填充参数< EM> putExtra &#39;第屏幕不需要从PC更改或再次更新,直到它们关闭为止。

但现在我需要一个可以从PC 动态更新的活动,同时显示它。我的应用程序中已经有一个线程,可以连续从PC接收命令。但是当PC的更新到来时,如何从该线程更新我的Activity?我应该注册某种广播,如果是的话,我的活动应该收听什么?我可以通过它传递参数,还是我需要将参数放在一个全球可访问的地方?

2 个答案:

答案 0 :(得分:2)

您可以使用BroadcastReceiver注册onStart(),然后在onStop()的{​​{1}}注销。

这样Activity就会知道有传入的更新。如果传入数据的大小很小(“a few KB”,那么您不会冒险在Android 7.0上使用Activity),如果它由原始数据类型组成,或者它是某种类型的{{ 1}}或TransactionTooLargeException然后您可以通过Parcelable将所有内容Serializable传递给Intent

另一种可能性是保存传入的数据(例如,保存到内部存储或SQLite数据库),并通过BroadcastReceiver通知Activity。然后Activity可以获取更新。

答案 1 :(得分:1)

  

我的应用程序中已经有一个线程从PC接收命令   连续

由于您要从单独的Activity更改Thread的内容,您需要在UI-Thread上实际更改Activity的内容。您可以在Application中定义一些静态方法,在这种情况下可能很有用:

public class ApplicationLoader extends Application {

    public static volatile Handler applicationHandler;

        @Override
        public void onCreate() {
            super.onCreate();
            applicationHandler = new Handler(getApplicationContext().getMainLooper());
        }

        public static void runOnUIThread(Runnable runnable) {
            applicationHandler.post(runnable);
        }
}

使用runOnUIThread(),您可以在Activity内调用一些方法来更新内容。请在Thread

中拨打此电话
ApplicationLoader.runOnUIThread(new Runnable() {
    @Override
    public void run() {
        activity.updateMyActivity();
    }
});

如果您可以访问Activity的实例,也可以使用内置的runOnUiThread()。上述解决方案适用于static个实例,但不推荐使用。

修改

我也有一些很好的来自Telegram的Observer代码:

public class NotificationCenter {

    private static int totalEvents = 1;

    public static final int updateActivity = totalEvents++;

    private final SparseArray<ArrayList<Object>> observers = new SparseArray<>();
    private final SparseArray<ArrayList<Object>> removeAfterBroadcast = new SparseArray<>();
    private final SparseArray<ArrayList<Object>> addAfterBroadcast = new SparseArray<>();

    private int broadcasting = 0;

    public interface NotificationCenterDelegate {
        void didReceivedNotification(int id, Object... args);
    }

    private static volatile NotificationCenter Instance = null;

    public static NotificationCenter getInstance() {
        NotificationCenter localInstance = Instance;
        if (localInstance == null) {
            synchronized (NotificationCenter.class) {
                localInstance = Instance;
                if (localInstance == null) {
                    Instance = localInstance = new NotificationCenter();
                }
            }
        }
        return localInstance;
    }

    public void postNotificationName(final int id, final Object... args) {
        if (Thread.currentThread() == ApplicationLoader.applicationHandler.getLooper().getThread()) {
            postNotificationNameInternal(id, args);
        } else {
            ApplicationLoader.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    postNotificationNameInternal(id, args);
                }
            });
        }
    }

    private void postNotificationNameInternal(int id, Object... args) {
        broadcasting++;
        ArrayList<Object> objects = observers.get(id);
        if (objects != null && !objects.isEmpty()) {
            for (int a = 0; a < objects.size(); a++) {
                Object obj = objects.get(a);
                ((NotificationCenterDelegate) obj).didReceivedNotification(id, args);
            }
        }
        broadcasting--;
        if (broadcasting == 0) {
            if (removeAfterBroadcast.size() != 0) {
                for (int a = 0; a < removeAfterBroadcast.size(); a++) {
                    int key = removeAfterBroadcast.keyAt(a);
                    ArrayList<Object> arrayList = removeAfterBroadcast.get(key);
                    for (int b = 0; b < arrayList.size(); b++) {
                        removeObserver(arrayList.get(b), key);
                    }
                }
                removeAfterBroadcast.clear();
            }
            if (addAfterBroadcast.size() != 0) {
                for (int a = 0; a < addAfterBroadcast.size(); a++) {
                    int key = addAfterBroadcast.keyAt(a);
                    ArrayList<Object> arrayList = addAfterBroadcast.get(key);
                    for (int b = 0; b < arrayList.size(); b++) {
                        addObserver(arrayList.get(b), key);
                    }
                }
                addAfterBroadcast.clear();
            }
        }
    }

    public void addObserver(Object observer, int id) {
        if (broadcasting != 0) {
            ArrayList<Object> arrayList = addAfterBroadcast.get(id);
            if (arrayList == null) {
                arrayList = new ArrayList<>();
                addAfterBroadcast.put(id, arrayList);
            }
            arrayList.add(observer);
            return;
        }
        ArrayList<Object> objects = observers.get(id);
        if (objects == null) {
            observers.put(id, (objects = new ArrayList<>()));
        }
        if (objects.contains(observer)) {
            return;
        }
        objects.add(observer);
    }

    public void removeObserver(Object observer, int id) {
        if (broadcasting != 0) {
            ArrayList<Object> arrayList = removeAfterBroadcast.get(id);
            if (arrayList == null) {
                arrayList = new ArrayList<>();
                removeAfterBroadcast.put(id, arrayList);
            }
            arrayList.add(observer);
            return;
        }
        ArrayList<Object> objects = observers.get(id);
        if (objects != null) {
            objects.remove(observer);
        }
    }
}

然后让你的Activity看起来像这样:

public class ScheduleActivity implements NotificationCenter.NotificationCenterDelegate {

    @Override
    public void onPause() {
        NotificationCenter.getInstance().removeObserver(this, NotificationCenter.updateActivity);
        super.onPause();
    }

    @Override
    public void onResume() {
        NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateActivity);
        super.onResume();
    }

    @Override
    public void didReceivedNotification(int id, Object... args) {
        if (id == NotificationCenter.updateActivity) {
            // update code, you can also define data in args
        }
    }
}

然后在Thread

NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateActivity, optionalData);