Advertisement
Guest User

Untitled

a guest
May 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. class ClipboardService extends IntentService {
  2. private static String TAG = "ClipboardService";
  3.  
  4. public ClipboardService() {
  5. super("ClipboardService");
  6. }
  7.  
  8. /* Define service as sticky so that it stays in background */
  9. @Override
  10. public int onStartCommand(Intent intent, int flags, int startId) {
  11. super.onStartCommand(intent, flags, startId);
  12. return START_STICKY;
  13. }
  14.  
  15. @Override
  16. public void onCreate() {
  17. super.onCreate();
  18. // start itself to ensure our broadcast receiver is active
  19. Log.d(TAG, "Start clipboard service.");
  20. startService(new Intent(getApplicationContext(), ClipboardService.class));
  21. }
  22.  
  23. /**
  24. * The IntentService calls this method from the default worker thread with
  25. * the intent that started the service. When this method returns, IntentService
  26. * stops the service, as appropriate.
  27. */
  28. @Override
  29. protected void onHandleIntent(Intent intent) {
  30. }
  31. }
  32.  
  33. public class ClipperReceiver extends BroadcastReceiver {
  34. private static String TAG = "ClipboardReceiver";
  35.  
  36. public static String ACTION_GET = "clipper.get";
  37. public static String ACTION_GET_SHORT = "get";
  38. public static String ACTION_SET = "clipper.set";
  39. public static String ACTION_SET_SHORT = "set";
  40. public static String EXTRA_TEXT = "text";
  41.  
  42. public static boolean isActionGet(final String action) {
  43. return ACTION_GET.equals(action) || ACTION_GET_SHORT.equals(action);
  44. }
  45.  
  46. public static boolean isActionSet(final String action) {
  47. return ACTION_SET.equals(action) || ACTION_SET_SHORT.equals(action);
  48. }
  49.  
  50. @Override
  51. public void onReceive(Context context, Intent intent) {
  52. ClipboardManager cb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
  53. if (isActionSet(intent.getAction())) {
  54. Log.d(TAG, "Setting text into clipboard");
  55. String text = intent.getStringExtra(EXTRA_TEXT);
  56. if (text != null) {
  57. cb.setText(text);
  58. setResultCode(Activity.RESULT_OK);
  59. setResultData("Text is copied into clipboard.");
  60. } else {
  61. setResultCode(Activity.RESULT_CANCELED);
  62. setResultData("No text is provided. Use -e text "text to be pasted"");
  63. }
  64. } else if (isActionGet(intent.getAction())) {
  65. Log.d(TAG, "Getting text from clipboard");
  66. CharSequence clip = cb.getText();
  67. if (clip != null) {
  68. Log.d(TAG, String.format("Clipboard text: %s", clip));
  69. setResultCode(Activity.RESULT_OK);
  70. setResultData(clip.toString());
  71. } else {
  72. setResultCode(Activity.RESULT_CANCELED);
  73. setResultData("");
  74. }
  75. }
  76. }
  77. }
  78.  
  79. am broadcast -a clipper.set -e text "1-line text"
  80.  
  81. am broadcast -a clipper.set -e text "2-linentext"
  82.  
  83. am broadcast -a clipper.set -e text "2-linerntext"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement