如果是以服务启动的话可以在Service的onStart()中使用startForeground (int id, Notification notification),传入Notification之后通知将置顶显示并不能清除。
你的问题不太清楚,但愿能帮到吧。
Google给出的例子:(兼容API5+)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384private static final Class[] mSetForegroundSignature = new Class[] {boolean.class}private static final Class[] mStartForegroundSignature = new Class[] {int.class, Notification.class}private static final Class[] mStopForegroundSignature = new Class[] {boolean.class}private NotificationManager mNMprivate Method mSetForegroundprivate Method mStartForegroundprivate Method mStopForegroundprivate Object[] mSetForegroundArgs = new Object[1]private Object[] mStartForegroundArgs = new Object[2]private Object[] mStopForegroundArgs = new Object[1]void invokeMethod(Method method, Object[] args) {try {method.invoke(this, args) } catch (InvocationTargetException e) {// Should not happen.Log.w("ApiDemos", "Unable to invoke method", e) } catch (IllegalAccessException e) {// Should not happen.Log.w("ApiDemos", "Unable to invoke method", e) }}/** * This is a wrapper around the new startForeground method, using the older * APIs if it is not available. */void startForegroundCompat(int id, Notification notification) {// If we have the new startForeground API, then use it.if (mStartForeground != null) {mStartForegroundArgs[0] = Integer.valueOf(id) mStartForegroundArgs[1] = notification invokeMethod(mStartForeground, mStartForegroundArgs) return }// Fall back on the old API.mSetForegroundArgs[0] = Boolean.TRUE invokeMethod(mSetForeground, mSetForegroundArgs) mNM.notify(id, notification)}/** * This is a wrapper around the new stopForeground method, using the older * APIs if it is not available. */void stopForegroundCompat(int id) {// If we have the new stopForeground API, then use it.if (mStopForeground != null) {mStopForegroundArgs[0] = Boolean.TRUE invokeMethod(mStopForeground, mStopForegroundArgs) return }// Fall back on the old API. Note to cancel BEFORE changing the// foreground state, since we could be killed at that point.mNM.cancel(id) mSetForegroundArgs[0] = Boolean.FALSE invokeMethod(mSetForeground, mSetForegroundArgs)}@Overridepublic void onCreate() {mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE) try {mStartForeground = getClass().getMethod("startForeground",mStartForegroundSignature) mStopForeground = getClass().getMethod("stopForeground",mStopForegroundSignature) return } catch (NoSuchMethodException e) {// Running on an older platform.mStartForeground = mStopForeground = null }try {mSetForeground = getClass().getMethod("setForeground",mSetForegroundSignature) } catch (NoSuchMethodException e) {throw new IllegalStateException("OS doesn't have Service.startForeground OR Service.setForeground!") }}@Overridepublic void onDestroy() {// Make sure our notification is gone.stopForegroundCompat(R.string.foreground_service_started)}
有一下几种方式:1、通过RelativeLayout的方式,设置一下属性中其中一个
android:layout_alignParentLeft
贴紧父元素的左边缘
android:layout_alignParentRight
贴紧父元素的右边缘
android:layout_alignParentTop
贴紧父元素的上边缘
android:layout_alignWithParentIfMissing
2、通过FrameLayout布局方式,最后一个添加该button就是置于上层。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)