安卓怎么置顶后台应用

安卓怎么置顶后台应用,第1张

是指要在通知中置顶显示吗?

如果是以服务启动的话可以在Service的onStart()中使用startForeground (int id, Notification notification),传入Notification之后通知将置顶显示并不能清除。

你的问题不太清楚,但愿能帮到吧。

Google给出的例子:(兼容API5+)

private static final Class<>[] mSetForegroundSignature = new Class[] {
    booleanclass};
private static final Class<>[] mStartForegroundSignature = new Class[] {
    intclass, Notificationclass};
private static final Class<>[] mStopForegroundSignature = new Class[] {
    booleanclass};
private NotificationManager mNM;
private Method mSetForeground;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mSetForegroundArgs = new Object[1];
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
void invokeMethod(Method method, Object[] args) {
    try {
        methodinvoke(this, args);
    } catch (InvocationTargetException e) {
        // Should not happen
        Logw("ApiDemos", "Unable to invoke method", e);
    } catch (IllegalAccessException e) {
        // Should not happen
        Logw("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] = IntegervalueOf(id);
        mStartForegroundArgs[1] = notification;
        invokeMethod(mStartForeground, mStartForegroundArgs);
        return;
    }
    // Fall back on the old API
    mSetForegroundArgs[0] = BooleanTRUE;
    invokeMethod(mSetForeground, mSetForegroundArgs);
    mNMnotify(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] = BooleanTRUE;
        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
    mNMcancel(id);
    mSetForegroundArgs[0] = BooleanFALSE;
    invokeMethod(mSetForeground, mSetForegroundArgs);
}
@Override
public 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 ServicestartForeground OR ServicesetForeground!");
    }
}
@Override
public void onDestroy() {
    // Make sure our notification is gone
    stopForegroundCompat(Rstringforeground_service_started);
}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/13072911.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-29
下一篇2025-08-29

发表评论

登录后才能评论

评论列表(0条)

    保存