Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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) {
- }
- }
- 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-linentext"
- am broadcast -a clipper.set -e text "2-linerntext"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement