api级别8的Android剪贴板管理器

时间:2014-12-30 10:46:48

标签: android

在我的应用程序中我想使用剪贴板管理器,在清单中我的min 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;

1 个答案:

答案 0 :(得分:1)

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

@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);
}

希望这有帮助!