在我的应用程序中,我想使用剪贴板管理器,在清单中我的最小SDK为8,而对于使用ClipBoardManager,我得到此错误:

Description Resource    Path    Location    Type
Call requires API level 11 (current min is 8): android.content.ClipData#newPlainText
Call requires API level 11 (current min is 8): android.content.ClipboardManager#setPrimaryClip
Call requires API level 11 (current min is 8): android.content.ClipData#newPlainText
Call requires API level 11 (current min is 8): android.content.ClipboardManager#setPrimaryClip
Call requires API level 11 (current min is 8): android.content.ClipData#newPlainText
Call requires API level 11 (current min is 8): android.content.ClipboardManager#setPrimaryClip
Call requires API level 11 (current min is 8): android.content.ClipboardManager#setPrimaryClip
Call requires API level 11 (current min is 8): android.content.ClipData#newPlainText


我的代码:

case 3:
    String n1 = numbers_list.getText ().toString ();
    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) G.context.getSystemService(G.context.CLIPBOARD_SERVICE);
        clipboard.setText(n1);
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) G.context.getSystemService(G.context.CLIPBOARD_SERVICE);
        android.content.ClipData clip_data = android.content.ClipData.newPlainText(
        "TSMS", n1);
        clipboard.setPrimaryClip(clip_data);
    }
    numbers_list.setText ( "" );
    break;

最佳答案

您需要在代码中添加抑制警告:

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")


使用以下代码:

    int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
    clipboard.setText(n1);
} else {
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
    android.content.ClipData clip_data = android.content.ClipData.newPlainText(
    context.getResources().getString(
    R.string.message), n1);
    clipboard.setPrimaryClip(clip_data);
}


希望这可以帮助!

10-04 18:53