2012-08-06 77 views
3

我嘗試在eclipse中運行android源代碼。這個應用程序的targetSdkVerison是5。我完全陌生的Android,並安裝了最新的API(Android 4.1)。在代碼的某些部分我m到處錯誤,如調用需要API級別5(當前最小值爲3):android.app.Service#startForeground

public void hideNotification(Service context) 
    { 
     context.setForeground(false);// error: The method setForeground(boolean) is undefined for the type Service 
     getNotificationManager(context).cancel(NOTIF_PLAYING); 
    } 



    public void showNotification(Service context, long songId) 
    { 
     context.startForeground(NOTIF_PLAYING, newNotification(context, songId));//error :Call requires API level 5 (current min is 3): android.app.Service#startForeground 
    } 

我想這些都是因爲不同的SDK版本之間的不兼容。我該如何解決這個問題?

回答

2

首先,setForeground(...)已被棄用,而不再可用。有關更多詳細信息,請參閱this blog

您收到此錯誤,因爲你最小的SDK級別,在清單中定義,是3 startForeground(...)需要API 5+。

現在,爲了解決這個問題,你可以不支持低於API 5東西;根據Dashboards,99.3%的用戶已經遷移到API 5+。爲此,請將您的minSdkVersion設置爲5,並將您的電話刪除至setForeground(...)。 (這將解決這兩個錯誤消息。)

setForeground(...)已經從SDK刪除,不應該再被使用,以往,在所有。

0

Service.setForeground(boolean)是自API級別5以來不存在的方法。它被替換爲startForeground(int, Notification)

的文檔(從鏈接)認爲可以使用,以支持較舊的設備,同時仍定位的API是5或更大的方法。

相關問題