【问题标题】:Android reboots when I try to set a service foreground当我尝试设置服务前台时,Android 会重新启动
【发布时间】:2011-03-16 12:11:03
【问题描述】:

我在我的应用程序中遇到了一个奇怪的行为:当我尝试设置服务前台时,手机(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

谢谢。

【问题讨论】:

    标签: android service reboot foreground cyanogenmod


    【解决方案1】:

    看看这个 Android API Demo here。请注意,它不是调用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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多