Java Code Examples for android.content.ClipboardManager#addPrimaryClipChangedListener()

The following examples show how to use android.content.ClipboardManager#addPrimaryClipChangedListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigBangService.java    From BigBang with Apache License 2.0 8 votes vote down vote up
/**
 * 监听剪切板
 */
private void listenClipboard() {
    final ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
        @Override
        public void onPrimaryClipChanged() {
            if (BigBangActivity.isShowing) {
                //是BigBang复制的,复制结束了就发广播关闭
                sendBroadcast(new Intent(Constant.FINISH_BIGBANG_ACTIVITY));
            } else {
                Intent intent = new Intent(getApplication(), BigBangActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Constant.CLIPBOARD_TEXT, clipboard.getText().toString());
                startActivity(intent);
            }
        }
    });
}
 
Example 2
Source File: CBWatcherService.java    From Clip-Stack with MIT License 8 votes vote down vote up
@Override
public void onCreate() {
    Log.v(MyUtil.PACKAGE_NAME, "onCreate CBService");
    mContext = this;
    mHandler = new Handler();
    preference = PreferenceManager.getDefaultSharedPreferences(this);
    readPreference();
    notificationManager = NotificationManagerCompat.from(this);
    db = Storage.getInstance(this.getBaseContext());
    clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    clipboardManager.addPrimaryClipChangedListener(listener);
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Log.w(MyUtil.PACKAGE_NAME, "Not support JobScheduler");
    } else {
        bindJobScheduler();
    }
    super.onCreate();
}
 
Example 3
Source File: TextCaptureService.java    From multi-copy with Apache License 2.0 5 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    if (textCapture_LOG)
    Log.d(TAG, "onStartCommand: ");
    toggleService = true;
    final ArrayList<String> copiedDataArray = new ArrayList<>();


        clipboardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                if (textCapture_LOG)
                Log.d(TAG, "onPrimaryClipChanged: before changing");
                if(toggleService) {
                    if (textCapture_LOG)
                    Log.d(TAG, "onPrimaryClipChanged: ");
                    final ClipData clipData = clipboardManager.getPrimaryClip();
                    if (clipData.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)
                            || clipData.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML)
                            || clipData.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
                        ClipData.Item item = clipData.getItemAt(0);
                        String thisText = item.getText().toString();
                        String sb = "";
                        copiedDataArray.add(thisText);
                        for (String text : copiedDataArray) {
                            sb += text + "\n";
                        }

                        ClipData copiedClip = ClipData.newPlainText("copiedClip", sb);
                        clipboardManager.removePrimaryClipChangedListener(this);
                        clipboardManager.setPrimaryClip(copiedClip);
                        clipboardManager.addPrimaryClipChangedListener(this);
                        Toast.makeText(TextCaptureService.this, "Multi Copied", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(TextCaptureService.this, "only text is allowed", Toast.LENGTH_SHORT).show();
                }
            }
        });

    return super.onStartCommand(intent, flags, startId);
}
 
Example 4
Source File: QQSimulateBusiness.java    From AutoInteraction-Library with Apache License 2.0 5 votes vote down vote up
private void readyClipboard(ClipboardManager.OnPrimaryClipChangedListener listener) {
    ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    if (null != mPrimaryClipChangedListener) {
        clipboardManager.removePrimaryClipChangedListener(mPrimaryClipChangedListener);
    }
    mPrimaryClipChangedListener = listener;
    clipboardManager.addPrimaryClipChangedListener(mPrimaryClipChangedListener);
}
 
Example 5
Source File: MyClipBoard.java    From XposedNavigationBar with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 开始监听剪贴板
 */
public static void startListenClipboard(final Context context) {

    final ClipboardManager clipboard = (ClipboardManager) context.
            getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
        @Override
        public void onPrimaryClipChanged() {
            //  XpLog.i("onPrimaryClipChanged");
            //获取剪贴板内容,先判断该内容是否为空
            if (clipboard.hasPrimaryClip()) {
                ClipData clipData = clipboard.getPrimaryClip();
                int count = clipData.getItemCount();
                for (int i = 0; i < count; ++i) {

                    ClipData.Item item = clipData.getItemAt(i);
                    CharSequence str = item
                            .coerceToText(context);
                    //因为复制历史记录里面某一条文字到剪贴板的时候,也会导致剪贴板内容变动,此处避免 添加重复内容到剪贴板历史
                    if (!clipboardData.contains(str.toString())) {
                        clipboardData.add(str.toString());
                    }
                }
            }
        }
    });
}
 
Example 6
Source File: ClipBoardActivity.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clip_board);

    mClipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    findViewById(R.id.copy).setOnClickListener(this);
    findViewById(R.id.paste).setOnClickListener(this);
    copyEdit = findViewById(R.id.copyEdit);
    pasteEdit = findViewById(R.id.pasteEdit);

    mClipboardManager.addPrimaryClipChangedListener(() -> Log.e(TAG, "onPrimaryClipChanged: " ));

}
 
Example 7
Source File: TranslateService.java    From Akoi-Translator with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
	super.onCreate();
	AppLog.i(LOGTAG, "onCreate");
	mClipManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
	mClipManager.addPrimaryClipChangedListener(this);
	mTranslator.registerOnTranslateResultListener(this);
}
 
Example 8
Source File: Clipboard.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private Clipboard() {
    mContext = ContextUtils.getApplicationContext();
    mClipboardManager =
            (ClipboardManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.CLIPBOARD_SERVICE);
    mClipboardManager.addPrimaryClipChangedListener(this);
}
 
Example 9
Source File: ClipboardMonitorService.java    From Android-Clipboard-Monitor with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // TODO: Show an ongoing notification when this service is running.
    mHistoryFile = new File(getExternalFilesDir(null), FILENAME);
    mClipboardManager =
            (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    mClipboardManager.addPrimaryClipChangedListener(
            mOnPrimaryClipChangedListener);
}
 
Example 10
Source File: WebViewActivity.java    From movienow with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void processLogic(Bundle savedInstanceState) {
    if (!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this);
    }
    //初始化监听剪贴板
    final ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    hasPrimaryClip = new ClipboardManager.OnPrimaryClipChangedListener() {
        @Override
        public void onPrimaryClipChanged() {
            long now = System.currentTimeMillis();
            if (now - previousTime < 5000) {
                return;
            }
            previousTime = now;
            if (manager == null || !manager.hasPrimaryClip()) {
                return;
            }
            //如果是文本信息
            if (manager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)
                    || manager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML)) {
                ClipData cdText = manager.getPrimaryClip();
                ClipData.Item item = cdText.getItemAt(0);
                //此处是TEXT文本信息
                if (item.getText() != null) {
                    final String t = item.getText().toString();
                    Snackbar.make(webViewT, "剪贴板被使用,是否撤销内容", Snackbar.LENGTH_LONG)
                            .setAction("撤销", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Helper.copyToClipboard(getContext(), text);
                                }
                            }).addCallback(new Snackbar.Callback() {
                        @Override
                        public void onDismissed(Snackbar transientBottomBar, int event) {
                            text = t;
                            super.onDismissed(transientBottomBar, event);
                        }
                    }).show();
                }
            }
        }
    };
    if (manager != null) {
        manager.addPrimaryClipChangedListener(hasPrimaryClip);
    }
}
 
Example 11
Source File: CBWatcherService.java    From ankihelper with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreate() {
    pm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    pm.addPrimaryClipChangedListener(listener);
}
 
Example 12
Source File: PGPClipperService.java    From PGPClipper with Apache License 2.0 4 votes vote down vote up
private void startClipboardMonitoring() {
    clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

    if (clipboardManager != null) {
        clipboardManager.addPrimaryClipChangedListener(onPrimaryClipChangedListener = new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                Log.d("PGPClipperService", "Clipboard data change detected!");

                // get current clipboard data to string
                String currentData;

                if (clipboardManager.hasPrimaryClip() && Objects.requireNonNull(clipboardManager.getPrimaryClipDescription()).hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

                    try {
                        currentData = Objects.requireNonNull(clipboardManager.getPrimaryClip()).getItemAt(0).getText().toString();
                    } catch (NullPointerException e) {
                        // should not happen since clipboard contains text, but returned null.
                        // best effort String parsing

                        try {
                            currentData = clipboardManager.getPrimaryClip().getItemAt(0).coerceToText(PGPClipperService.this).toString();

                        } catch (Exception e2) {
                            // best attempt failed... return this method

                            return;
                        }

                    }
                } else {
                    return;
                }

                // tidy once
                currentData = PGPBlockDetector.pgpInputTidy(currentData);

                // check if this contains ASCII armored PGP data

                if (PGPBlockDetector.isBlockPresent(currentData)) {
                    // notify user

                    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                    Intent intentToLaunchWhenNotificationClicked = new Intent(getApplicationContext(), PGPClipperService.class);
                    intentToLaunchWhenNotificationClicked.putExtra(TRY_DECRYPT, true);
                    intentToLaunchWhenNotificationClicked.putExtra(DATA, currentData);

                    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intentToLaunchWhenNotificationClicked, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT);

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            CharSequence name = getString(R.string.channel_name);
                            String description = getString(R.string.channel_description);
                            int importance = NotificationManager.IMPORTANCE_HIGH;
                            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                            channel.setDescription(description);
                            // Register the channel with the system; you can't change the importance
                            // or other notification behaviors after this
                            notificationManager.createNotificationChannel(channel);
                        }

                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                            .setSmallIcon(R.drawable.ic_noti)
                            .setTicker(getString(R.string.NotificationTickerText))
                            .setContentTitle(getString(R.string.NotificationTitleText))
                            .setContentText(getString(R.string.NotificationContentText))
                            .setDefaults(Notification.DEFAULT_LIGHTS)
                            .setContentIntent(pendingIntent)
                            .setPriority(NotificationCompat.PRIORITY_MAX)
                            .setWhen(System.currentTimeMillis())
                            .setAutoCancel(true);

                    notificationManager.notify(8591274, notificationBuilder.build());
                }

            }
        });
    }

}