在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);
+