如何在android中获取浮动按钮的徽章计数?

时间:2015-12-01 09:06:44

标签: android

我已经在下面提到了链接以获取浮动按钮。但我想在浮动按钮上显示徽章计数。(浮动按钮的调整方式是我们可以在屏幕上移动它)。我如何设置徽章计数呢? 这里没有onclick我想显示带文字的浮动图标(徽章)。我怎么能这样做?

https://droidqd.wordpress.com/2015/01/07/how-to-make-a-floating-face-bubble-on-home-screen-in-android/

 Edit :

This is the logcat output:

    12-01 18:32:09.943: D/AndroidRuntime(1252): Shutting down VM
12-01 18:32:09.943: D/AndroidRuntime(1252): --------- beginning of crash
12-01 18:32:10.033: E/AndroidRuntime(1252): FATAL EXCEPTION: main
12-01 18:32:10.033: E/AndroidRuntime(1252): Process: com.example.floatingbubble, PID: 1252
12-01 18:32:10.033: E/AndroidRuntime(1252): java.lang.IllegalArgumentException: View=android.widget.ImageView{30ba5217 V.ED.... ......I. 0,0-72,72} not attached to window manager
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:396)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:322)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:111)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at com.example.floatingbubble.FloatingFaceBubbleService$1.onTouch(FloatingFaceBubbleService.java:67)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.View.dispatchTouchEvent(View.java:8465)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.View.dispatchPointerEvent(View.java:8666)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.os.MessageQueue.nativePollOnce(Native Method)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.os.MessageQueue.next(MessageQueue.java:143)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.os.Looper.loop(Looper.java:122)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at android.app.ActivityThread.main(ActivityThread.java:5257)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at java.lang.reflect.Method.invoke(Native Method)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at java.lang.reflect.Method.invoke(Method.java:372)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
12-01 18:32:10.033: E/AndroidRuntime(1252):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-01 18:32:10.128: I/art(1252): Background partial concurrent mark sweep GC freed 888(178KB) AllocSpace objects, 1(20KB) LOS objects, 56% free, 395KB/907KB, paused 1.900ms total 189.650ms
12-01 18:32:24.830: I/Process(1252): Sending signal. PID: 1252 SIG: 9
12-01 18:32:28.157: D/OpenGLRenderer(1327): Use EGL_SWAP_BEHAVIOR_PRESERVED: true

1 个答案:

答案 0 :(得分:1)

你需要为FloatingBubbleService设计一个布局,只需添加一个视图,即ImageView,你可以做的就是设计一个你希望服务看起来的布局......和您可以添加ImageViewLinearLayout,而不是添加RelativeLayout

例如。

private TextView tv;
private RelativeLayout rl;
//code..
//inside your onCreate()
rl = new RelativeLayout(context);
tv = new TextView(context);
tv.setText(badgeCount+"");//Badge Count
rl.addView(floatingFaceBubble);
rl.addView(tv);
//and instead of windowManager.addView(floatingFaceBubble, myParams);
windowManager.addView(rl, myParams);

注意:上面的代码不会按原样运行..但是这里只是为了让您了解如何解决问题。

每当您需要更新Textview的文字时,只需按tv.setText()拨打电话就可以了解您的问题。

//instead of floatingFaceBubble.setOnTouchListener use
rl.setOnTouchListener
//and instead of this windowManager.removeView(floatingFaceBubble); use
windowManager.removeView(rl);