Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BootCompletedIntentListener extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
- Intent serviceIntent = new Intent(context,ClipMonitorService.class);
- ContextCompat.startForegroundService(context,serviceIntent);
- }
- }
- }
- public class ClipMonitorService extends Service {
- private static final String TAG = "ClipboardManager";
- private ExecutorService mThreadPool = Executors.newSingleThreadExecutor();
- private ClipboardManager mClipboardManager;
- private PrefManager prefManager;
- @Override
- public void onCreate() {
- super.onCreate();
- prefManager = new PrefManager(this);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (mClipboardManager != null) {
- mClipboardManager.removePrimaryClipChangedListener(
- mOnPrimaryClipChangedListener);
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Intent notificationIntent = new Intent(this, MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
- Intent settingIntent = new Intent(this, SettingActivity.class);
- PendingIntent pendingSettIntent = PendingIntent.getActivity(this, 0, settingIntent, 0);
- RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
- remoteViews.setOnClickPendingIntent(R.id.btn_action, pendingSettIntent);
- remoteViews.setTextViewText(R.id.notif_subtitle, "1 Clips copied Today");
- Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
- .setSmallIcon(R.mipmap.ic_launcher_round)
- .setContent(remoteViews)
- .setVisibility(Notification.VISIBILITY_SECRET)
- .setPriority(NotificationCompat.PRIORITY_MIN)
- .setContentIntent(pendingIntent)
- .setColor(getResources().getColor(R.color.colorPrimary))
- .setShowWhen(false)
- .build();
- startForeground(1, notification);
- mClipboardManager =
- (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
- mClipboardManager.addPrimaryClipChangedListener(
- mOnPrimaryClipChangedListener);
- return START_STICKY;
- }
- private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
- new ClipboardManager.OnPrimaryClipChangedListener() {
- @Override
- public void onPrimaryClipChanged() {
- Log.d(TAG, "onPrimaryClipChangeds");
- try {
- String textToPaste = mClipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
- if (textToPaste.length() > 200) {
- if (prefManager.isClipNotifOns()) {
- mThreadPool.execute(new MakeNotifRunnable(
- textToPaste));
- }
- }
- } catch (Exception ignored) {
- }
- }
- };
- private class MakeNotifRunnable implements Runnable {
- private final CharSequence mTextToWrite;
- public MakeNotifRunnable(CharSequence text) {
- mTextToWrite = text;
- }
- @Override
- public void run() {
- Intent notifIntent = new Intent(getApplicationContext(), PostNewsActivity.class);
- notifIntent.putExtra("post", mTextToWrite);
- NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
- int notificationId = 2;
- String channelId = "channel1";
- String channelName = "Clipboard Monitor Notification";
- int importance = 0;
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
- importance = NotificationManager.IMPORTANCE_DEFAULT;
- }
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- NotificationChannel mChannel = new NotificationChannel(
- channelId, channelName, importance);
- notificationManager.createNotificationChannel(mChannel);
- }
- NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId)
- .setSmallIcon(R.mipmap.ic_launcher_round)
- .setContentTitle("Verify copied content")
- .setContentText(mTextToWrite)
- .setAutoCancel(true)
- .setOnlyAlertOnce(true);
- TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
- stackBuilder.addNextIntent(notifIntent);
- PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
- 0,
- PendingIntent.FLAG_UPDATE_CURRENT
- );
- mBuilder.setContentIntent(resultPendingIntent);
- notificationManager.notify(notificationId, mBuilder.build());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement