剪贴板:设置多行

时间:2016-05-25 23:13:27

标签: java android android-intent adb clipboard

我有一个应用程序,我可以使用它来设置或通过adb获取剪贴板。但是,它没有正确设置换行符。

我认为问题在于服务只是将剪贴板的内容转储为单个日志消息,而这些消息显示在一行中。

我如何将文本分成行并单独记录这些行?

[ClipboardService]

class ClipboardService extends IntentService {
    private static String TAG = "ClipboardService";

    public ClipboardService() {
        super("ClipboardService");
    }

    /* Define service as sticky so that it stays in background */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // start itself to ensure our broadcast receiver is active
        Log.d(TAG, "Start clipboard service.");
        startService(new Intent(getApplicationContext(), ClipboardService.class));
    }

    /**
     * The IntentService calls this method from the default worker thread with
     * the intent that started the service. When this method returns, IntentService
     * stops the service, as appropriate.
     */
    @Override
    protected void onHandleIntent(Intent intent) {
    }
}

[ClipperReceiver.java]

public class ClipperReceiver extends BroadcastReceiver {
    private static String TAG = "ClipboardReceiver";

    public static String ACTION_GET = "clipper.get";
    public static String ACTION_GET_SHORT = "get";
    public static String ACTION_SET = "clipper.set";
    public static String ACTION_SET_SHORT = "set";
    public static String EXTRA_TEXT = "text";

    public static boolean isActionGet(final String action) {
        return ACTION_GET.equals(action) || ACTION_GET_SHORT.equals(action);
    }

    public static boolean isActionSet(final String action) {
        return ACTION_SET.equals(action) || ACTION_SET_SHORT.equals(action);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ClipboardManager cb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (isActionSet(intent.getAction())) {
            Log.d(TAG, "Setting text into clipboard");
            String text = intent.getStringExtra(EXTRA_TEXT);
            if (text != null) {
                cb.setText(text);
                setResultCode(Activity.RESULT_OK);
                setResultData("Text is copied into clipboard.");
            } else {
                setResultCode(Activity.RESULT_CANCELED);
                setResultData("No text is provided. Use -e text \"text to be pasted\"");
            }
        } else if (isActionGet(intent.getAction())) {
            Log.d(TAG, "Getting text from clipboard");
            CharSequence clip = cb.getText();
            if (clip != null) {
                Log.d(TAG, String.format("Clipboard text: %s", clip));
                setResultCode(Activity.RESULT_OK);
                setResultData(clip.toString());
            } else {
                setResultCode(Activity.RESULT_CANCELED);
                setResultData("");
            }
        }
    }
}

像这样设置剪贴板可以正常工作:

am broadcast -a clipper.set -e text "1-line text"

但是将剪贴板设置为例如:

am broadcast -a clipper.set -e text "2-line\ntext"

直接复制文本并粘贴它会给出:“2-line \ ntext” 我也试过这个:

am broadcast -a clipper.set -e text "2-line\r\ntext"

但结果相同。

0 个答案:

没有答案