2011-03-16 96 views
1

我在應用程序中遇到了一個奇怪的行爲:當我嘗試設置服務前景時,手機(HTC G1 + Cyanogen Mod)重新啓動。當我嘗試設置服務前景時Android重新啓動

當我不嘗試前景這個服務時,它可以工作。

這裏是牽連代碼:

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

感謝。

回答

0

看看這個Android API演示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); 
} 
+0

使得SENS ...謝謝 – Rob 2011-03-17 04:15:23

相關問題