开发者

Android reboots when I try to set a service foreground

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开发者_如何学Go = 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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜