在Activity的子類中,可以直接用 runOnUiThread() , 但是如果是在普通類中,就沒這個方法能調用了,最簡單的寫法是用Handler getMainLooper,下邊貼上對比runOnUiThread的寫法:
- runOnUiThread(new Runnable() {
- public void run() {
- wmOne.addView(floatView, params);
- ObjectAnimator a = ObjectAnimator.ofFloat(floatView.rootView, "translationY", -700, 0);
- a.setDuration(600);
- a.start();
- floatView.setNotification(headsUp);
- if (headsUp.getNotification() != null) {
- headsUp.getBuilder().setFullScreenIntent(null, false);
- notificationManager.notify(headsUp.getCode(), headsUp.getNotification());
- }
- }
- });
+ new Handler(context.getMainLooper())
+ .post(()-> {
+ wmOne.addView(floatView, params);
+ ObjectAnimator a = ObjectAnimator.ofFloat(floatView.rootView, "translationY", -700, 0);
+ a.setDuration(600);
+ a.start();
+ floatView.setNotification(headsUp);
+ if (headsUp.getNotification() != null) {
+ headsUp.getBuilder().setFullScreenIntent(null, false);
+ notificationManager.notify(headsUp.getCode(), headsUp.getNotification());
+ }
+ });