2011-11-08 21 views
7

android.text.ClipboardManager自API級別11棄用,並替換爲android.content.ClipboardManagersource)。處理棄用的android.text.ClipboardManager

如何編寫支持這兩種情況的代碼?導入android.content.ClipboardManager並在11+中使用該功能,但強制在10中關閉。將導入更改爲android.text.ClipboardManager會引發11+以上的棄用警告。

如何順利處理兩種情況?我需要導入什麼?

回答

6

我結束了使用舊的方式(android.text.ClipboardManager和this answer的代碼),以及一對@SuppressWarnings(「deprecation」)註釋。

+1

那麼關於棄用解決方案?? –

4

明確:

@SuppressWarnings("deprecation") 
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
    clipboard.setText(shareViaSMSBody); 

因爲這有繼續工作的舊設備,很可能是過時的代碼不會在Android中移除。

0

如果您仍然支持< SDK 11,您的工作量過大。設置最低爲15,使用此代碼:

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
ClipData clip = ClipData.newPlainText("label for text", "text to copy"); 
clipboard.setPrimaryClip(clip); 
+0

好吧,我的gf使用舊kindle,所以sdk 15不是一個選擇 – codingpuss

2

參考this answer

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context 
     .getSystemService(Context.CLIPBOARD_SERVICE); 
final android.content.ClipData clipData = android.content.ClipData 
     .newPlainText("text label", "text to clip"); 
clipboardManager.setPrimaryClip(clipData); 
} else { 
final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context 
     .getSystemService(Context.CLIPBOARD_SERVICE); 
clipboardManager.setText("text to clip"); 
} 
相關問題