当我尝试设置服务前台时 Android 会重新启动

发布于 2024-10-22 03:06:59 字数 613 浏览 4 评论 0原文

我在我的应用程序中遇到了一个奇怪的行为:当我尝试设置服务前台时,手机(HTC G1 + Cyanogen Mod)重新启动。

当我不尝试将此服务置于前台时它会起作用。

这是有罪的代码:

Notification notification = new Notification(R.drawable.icon,
    getText(R.string.ticker_text), System.currentTimeMillis());
    startForeground(SERVICE_NOTIFICATION_ID, notification);
Log.v(TAG, "Control service foregrounded.");

你能看出问题出在哪里吗?

如果您需要更多数据,可以在 GitHub 上找到整个项目: https: //github.com/rbochet/Serval-Video-Discovery/tree/network-remote

谢谢。

I've hit a strange behaviour in my app: when I try to set a service foreground, the handset (HTC G1 + Cyanogen Mod) reboots.

It works when I don't try to foreground this service.

Here is the incriminated code:

Notification notification = new Notification(R.drawable.icon,
    getText(R.string.ticker_text), System.currentTimeMillis());
    startForeground(SERVICE_NOTIFICATION_ID, notification);
Log.v(TAG, "Control service foregrounded.");

Can you see where is the problem?

If you need more data, the whole project can be found on GitHub: https://github.com/rbochet/Serval-Video-Discovery/tree/network-remote

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2024-10-29 03:06:59

此处查看此 Android API 演示。请注意,它不是调用 startForeground(),而是调用 startForegroundCompat(),这是一个根据新/旧 startForeground API 处理您的请求的包装器。

void handleCommand(Intent intent) {
        if (ACTION_FOREGROUND.equals(intent.getAction())) {
            ...

            // Set the icon, scrolling text and timestamp
            Notification notification = new Notification(R.drawable.stat_sample, text,
                    System.currentTimeMillis());

            // The PendingIntent to launch our activity if the user selects this notification
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, Controller.class), 0);

            // Set the info for the views that show in the notification panel.
            notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                           text, contentIntent);

            startForegroundCompat(R.string.foreground_service_started, notification);
            ...
    }

这是startForegroundCompat()

/**
 * 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);
}

Look at this Android API Demo here. Notice that rather than calling startForeground(), it calls startForegroundCompat(), a wrapper that handles your request depending on the new/old startForeground API.

void handleCommand(Intent intent) {
        if (ACTION_FOREGROUND.equals(intent.getAction())) {
            ...

            // Set the icon, scrolling text and timestamp
            Notification notification = new Notification(R.drawable.stat_sample, text,
                    System.currentTimeMillis());

            // The PendingIntent to launch our activity if the user selects this notification
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, Controller.class), 0);

            // Set the info for the views that show in the notification panel.
            notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                           text, contentIntent);

            startForegroundCompat(R.string.foreground_service_started, notification);
            ...
    }

Here is startForegroundCompat():

/**
 * 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文